Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean in shell when we put a command inside dollar sign and parentheses: $(command)

People also ask

What does dollar sign do in shell script?

The system shell prompt That dollar sign means: we're in the system shell, i.e the program that you're put into as soon as you open the Terminal app. The dollar sign is often the symbol used to signify where you can begin typing in commands (you should see a blinking cursor there).

What does $- mean in bash?

$- prints The current set of options in your current shell. himBH means following options are enabled: H - histexpand : when history expansion is enabled. m - monitor : when job control is enabled.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.


Usage of the $ like ${HOME} gives the value of HOME. Usage of the $ like $(echo foo) means run whatever is inside the parentheses in a subshell and return that as the value. In my example, you would get foo since echo will write foo to standard out


DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Could anybody help me to figure out how this command get executed?

Let's look at different parts of the command. BASH_SOURCE is a bash array variable containing source filenames. So "${BASH_SOURCE[0]}" would return you the name of the script file.

dirname is a utility provided by GNU coreutils that remove the last component from the filename. Thus if you execute your script by saying bash foo, "$( dirname "${BASH_SOURCE[0]}" )" would return .. If you said bash ../foo, it'd return ..; for bash /some/path/foo it'd return /some/path.

Finally, the entire command "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" gets the absolute directory containing the script being invoked.

$(...) allows command substitution, i.e. allows the output of a command to replace the command itself and can be nested.