What does $@ mean in a shell script?

$@ is all of the parameters passed to the script. For instance, if you call ./someScript.sh foo bar then $@ will be equal to foo bar. If you do: ./someScript.sh foo bar and then inside someScript.sh reference: umbrella_corp_options “$@” this will be passed to umbrella_corp_options with each individual parameter enclosed in double quotes, allowing to … Read more

“echo -n” prints “-n”

There are multiple versions of the echo command, with different behaviors. Apparently the shell used for your script uses a version that doesn’t recognize -n. The printf command has much more consistent behavior. echo is fine for simple things like echo hello, but I suggest using printf for anything more complicated. What system are you … Read more

How can I declare and use Boolean variables in a shell script?

Revised Answer (Feb 12, 2014) the_world_is_flat=true # …do something interesting… if [ “$the_world_is_flat” = true ] ; then echo ‘Be careful not to fall off!’ fi Original Answer Caveats: https://stackoverflow.com/a/21210966/89391 the_world_is_flat=true # …do something interesting… if $the_world_is_flat ; then echo ‘Be careful not to fall off!’ fi From: Using boolean variables in Bash The reason … Read more

How to read a file into a variable in shell?

In cross-platform, lowest-common-denominator sh you use: #!/bin/sh value=`cat config.txt` echo “$value” In bash or zsh, to read a whole file into a variable without invoking cat: #!/bin/bash value=$(<config.txt) echo “$value” Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat. Note that it is not necessary to … Read more

How can I have a newline in a string in sh?

If you’re using Bash, the solution is to use $’string’, for example: $ STR=$’Hello\nWorld’ $ echo “$STR” # quotes are required here! Hello World If you’re using pretty much any other shell, just insert the newline as-is in the string: $ STR=’Hello > World’ Bash is pretty nice. It accepts more than just \n in … Read more