Title: Redirect IO inside shell script
Tags: bash;shell
Question: I'm trying to recursively expand all ```.css``` files in a directory. I'm using beautifier (a node.js tool). It's supposed to to get a filename, expand its content, and write the result to stdout. So the following line do the job pretty well:

```./node_modules/.bin/beautifier ./css/foo.css &gt; ./css/bar.css 
```

But when I put all the things inside a script, it just prints all the output to the ```stdout``` and leaves all files empty after exit:

```echo "Expanding CSS files..."
for css in `find . -type f -name \*.css -print`
do
    echo "Expanding $css"
    temp="$css.tmp"
    cp $css $temp
    ./node_modules/.bin/beautifier $temp&gt;$css 
    rm $temp
done
```

what am I doing wrong here?

Comment: Try adding `set -ex` at the top of the script. It won't solve the problem, but it might help you find it.

Comment: Ok. Sorry, I wrote too fast. :)

Comment: Or just `#!/bin/bash -x` rather than whatever shebang your're currently using. Also note, you shouldn't do `for something in $(command)` in bash, it will incorrectly split files with spaces for example, see [this](http://mywiki.wooledge.org/ParsingLs). I would also add an `echo` in front of `./node_modules/.bin/beautifier` and put the '>' in single quotes to test specifically what it's trying to do.

Comment: @rpax No. I'm using a minifier script (which joins all lines together, removes indents and extra spaces). The beautufier does the reverse. Breaks lines, adds indents and formats the source code.

Comment: @Biffen seems shell removes the `>` from what to command. It runs something like: `./node_modules/.bin/beautifier ./css/bootstrap-theme.min.css.tmp ./css/bootstrap-theme.min.css`

Here is the accepted answer: Putting the variables inside ```""``` solves the problem

```echo "Expanding CSS files..."
for css in `find . -type f -name \*.css -print`
do
   echo "Expanding $css"
   temp="$css.tmp"
   cp $css $temp
   ./node_modules/.bin/beautifier "$temp" &gt; "$css" 
   rm $temp

done
```

EDIT: The real problem was there were no spaces between ```&gt;``` . But as @Biffen pointed, this is a good practice. If the line were like this,

```"$temp"&gt;"$css"
```

An error raises:

```unexpected EOF while looking for matching `"'
syntax error: unexpected end of file
```

So it's easier to debug.

Although my answer works, @Briffen is discovered the real error, the credit should go to him.

Comment for this answer: Which is good practice, too, since file names may contain spaces. You may want to add it to the `cp` and `rm` commands as well.
Comment for this answer: It (almost) never hurts. BTW, are you sure it was the quoting, and not the spaces around `>`?
Comment for this answer: I did it initially, but I I wanted to solve only the problematic line
Comment for this answer: @Biffen You are right. Are the spaces around `>`. But now, if the spaces are removed,
`unexpected EOF while looking for matching \`"'`
Comment for this answer: @Briffen see my edited answer. The only thing I can do is +1 to your comment, sorry.
Comment for this answer: Can you explain why this was the problem? You would never get files with spaces here anyway since they would get incorrectly split by `for css in $(find . -type f -name \*.css -print)`
Comment for this answer: Are you sure you're running it as a `bash` script? There shouldn't be any difference between `"$a" > "$b"` and `"$a">"b"` in bash. @Biffen was pointing out that variables should be quoted in bash, which is completely true, but would not have any effect here either since the script is broken to begin with as files with spaces would get split into separate tokens by the for loop.

Here is another answer: This isn't really an answer, but again your script will incorrectly split files with spaces. You should do something like below instead

```#!/bin/bash
echo "Expanding CSS files..."
while IFS= read -r -d '' css
do
   echo "Expanding $css"
   temp="$css.tmp"
   echo cp "$css" "$temp"
   echo ./node_modules/.bin/beautifier "$temp" '&gt;' "$css"
   echo rm "$temp"
done < <(find . -type f -name \*.css -print0)
```

I put ```echo```'s everywhere and quotes around ```&gt;``` so it doesn't actually do anything other than show you what it would do. Could also do ```#!/bin/bash -x``` which would show what it's doing while executing commands. 

Comment for this answer: Yes, do this rather than `for css in $(find [...])`.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:03/31/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<bash><shell>', 'Title': 'Redirect IO inside shell script', 'ViewCount': 77, 'id': 22759085, 'user_AboutMe': '<ul>\n<li>C/C++ Linux development</li>\n<li>Embedded Systems</li>\n<li>Electronics Hobbyist</li>\n<li>And I Love cats :&gt;</li>\n</ul>\n', 'user_AccountId': 102556.0, 'user_DisplayName': 'sorush-r', 'user_Reputation': 10348.0, 'user_Views': 4347.0, 'user_id': 275221.0}}