Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'cd ${0%/*}' mean in bash?

Tags:

bash

I encountered a git commit which cleans up some readlink and dirname command with this magic variable substitution cd ${0%/*}.

How does bash interpret it?

like image 687
steveyang Avatar asked Mar 06 '15 07:03

steveyang


People also ask

What is ${ 0 * in shell script?

${0} is the first argument of the script, i.e. the script name or path. If you launch your script as path/to/script.sh , then ${0} will be exactly that string: path/to/script.sh . The %/* part modifies the value of ${0} . It means: take all characters until / followed by a file name.

What is ${ 0 in bash?

If the $0 special variable is used within a Bash script, it can be used to print its name and if it is used directly within the terminal, it can be used to display the name of the current shell.

What does * mean in shell script?

$* expands to all parameters that were passed to that shell script. $0 = shell script's name. $1 = first argument. $2 = second argument ...etc. $# = number of arguments passed to shellscript.

What does ${ 1 mean in bash script?

- 1 means the first parameter passed to the function ( $1 or ${1} ) - # means the index of $1 , which, since $1 is an associative array, makes # the keys of $1.


1 Answers

The command cd ${0%/*} changes directory to the directory containing the script, assuming that $0 is set to the fully-qualified path of the script.

like image 119
Ross Ridge Avatar answered Sep 21 '22 15:09

Ross Ridge