




























































































Étudies grâce aux nombreuses ressources disponibles sur Docsity
Gagnz des points en aidant d'autres étudiants ou achete-les avec un plan Premium
Cette page n'est pas visible dans l'aperçu
Ne manques pas les parties importantes!





























































































Bash Notes for Professionals
GoalKicker.com – Bash Notes for Professionals 1
About
This Bash Notes for Professionals book is compiled from Stack Overflow Documentation, the content is written by the beautiful people at Stack Overflow. Text content is released under Creative Commons BY-SA, see credits at the end of this book whom contributed to the various chapters. Images may be copyright of their respective owners unless otherwise specified
This is an unofficial free book created for educational purposes and is not affiliated with official Bash group(s) or company(s) nor Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners
The information presented in this book is not guaranteed to be correct nor accurate, use at your own risk
Please send feedback and corrections to [email protected]
GoalKicker.com – Bash Notes for Professionals 2
Chapter 1: Getting started with Bash
Version Release Date 0.99 1989-06- 1.01 1989-06- 2.0 1996-12- 2.02 1998-04- 2.03 1999-02- 2.04 2001-03- 2.05b 2002-07- 3.0 2004-08- 3.1 2005-12- 3.2 2006-10- 4.0 2009-02- 4.1 2009-12- 4.2 2011-02- 4.3 2014-02- 4.4 2016-09-
Section 1.1: Hello World
Interactive Shell
The Bash shell is commonly used interactively: It lets you enter and edit commands, then executes them when you press the Return key. Many Unix-based and Unix-like operating systems use Bash as their default shell (notably Linux and macOS). The terminal automatically enters an interactive Bash shell process on startup.
Output Hello World by typing the following:
echo "Hello World" #> Hello World # Output Example
Notes
You can change the shell by just typing the name of the shell in terminal. For example: sh, bash , etc. echo is a Bash builtin command that writes the arguments it receives to the standard output. It appends a newline to the output, by default.
Non-Interactive Shell
The Bash shell can also be run non-interactively from a script, making the shell require no human interaction. Interactive behavior and scripted behavior should be identical – an important design consideration of Unix V Bourne shell and transitively Bash. Therefore anything that can be done at the command line can be put in a script file for reuse.
Follow these steps to create a Hello World script:
GoalKicker.com – Bash Notes for Professionals 4
needs to be on your PATH. On many systems, / bin /bash doesn't exist, and you should use / usr / local / bin /bash or some other absolute path; this change avoids having to figure out the details of that.
1 Also referred to as sha-bang, hashbang, pound-bang, hash-pling.
Section 1.2: Hello World Using Variables
Create a new file called hello.sh with the following content and give it executable permissions with chmod +x hello.sh.
Execute/Run via:. / hello.sh
#!/usr/bin/env bash
# Note that spaces cannot be used around the = assignment operator whom_variable="World"
# Use printf to safely output the data printf "Hello, %s \n " "$whom_variable" #> Hello, World
This will print Hello, World to standard output when executed.
To tell bash where the script is you need to be very specific, by pointing it to the containing directory, normally with ./ if it is your working directory, where. is an alias to the current directory. If you do not specify the directory, bash tries to locate the script in one of the directories contained in the $PATH environment variable.
The following code accepts an argument $1, which is the first command line argument, and outputs it in a formatted string, following Hello,.
Execute/Run via:. / hello.sh World
#!/usr/bin/env bash printf "Hello, %s \n " "$1" #> Hello, World
It is important to note that $1 has to be quoted in double quote, not single quote. "$1" expands to the first command line argument, as desired, while '$1' evaluates to literal string $1.
Security Note: Read Security implications of forgetting to quote a variable in bash shells to understand the importance of placing the variable text within double quotes.
Section 1.3: Hello World with User Input
The following will prompt a user for input, and then store that input as a string (text) in a variable. The variable is then used to give a message to the user.
GoalKicker.com – Bash Notes for Professionals 5
#!/usr/bin/env bash echo "Who are you?" read name echo "Hello, $name."
The command read here reads one line of data from standard input into the variable name. This is then referenced using $name and printed to standard out using echo.
Example output:
$. / hello_world.sh Who are you? Matt Hello, Matt.
Here the user entered the name "Matt", and this code was used to say Hello, Matt..
And if you want to append something to the variable value while printing it, use curly brackets around the variable name as shown in the following example:
#!/usr/bin/env bash echo "What are you doing?" read action echo "You are ${action}ing."
Example output:
$. / hello_world.sh What are you doing? Sleep You are Sleeping.
Here when user enters an action, "ing" is appended to that action while printing.
Section 1.4: Importance of Quoting in Strings
Quoting is important for string expansion in bash. With these, you can control how the bash parses and expands your strings.
There are two types of quoting:
Weak : uses double quotes: " Strong : uses single quotes: '
If you want to bash to expand your argument, you can use Weak Quoting :
#!/usr/bin/env bash world="World" echo "Hello $world" #> Hello World
If you don't want to bash to expand your argument, you can use Strong Quoting :
#!/usr/bin/env bash world="World" echo 'Hello $world'
GoalKicker.com – Bash Notes for Professionals 7
adding_string_to_number="s" v=$ (expr 5 + $adding_string_to_number )
$. / hello.sh Hello World
expr: non-integer argument
The above prompted error is not enough to trace the script; however, using the following way gives you a better sense where to look for the error in the script.
$ bash -x hello.sh
echo Hello World\n Hello World
adding_string_to_number=s
expr 5 + s expr: non-integer argument
v=
Section 1.7: Handling Named Arguments
#!/bin/bash
deploy= false uglify= false
while (( $# > 1 )) ; do case $1 in --deploy ) deploy="$2" ;; --uglify ) uglify="$2" *;; ) break ; esac ; shift 2 done
$deploy && echo "will deploy... deploy = $deploy" $uglify && echo "will uglify... uglify = $uglify"
_# how to run
GoalKicker.com – Bash Notes for Professionals 8
Chapter 2: Script shebang
Section 2.1: Env shebang
To execute a script file with the bash executable found in the PATH environment variable by using the executable env , the first line of a script file must indicate the absolute path to the env executable with the argument bash :
#!/usr/bin/env bash
The env path in the shebang is resolved and used only if a script is directly launch like this:
script.sh
The script must have execution permission.
The shebang is ignored when a bash interpreter is explicitly indicated to execute a script:
bash script.sh
Section 2.2: Direct shebang
To execute a script file with the bash interpreter, the first line of a script file must indicate the absolute path to the bash executable to use:
#!/bin/bash
The bash path in the shebang is resolved and used only if a script is directly launch like this:
. / script.sh
The script must have execution permission.
The shebang is ignored when a bash interpreter is explicitly indicated to execute a script:
bash script.sh
Section 2.3: Other shebangs
There are two kinds of programs the kernel knows of. A binary program is identified by it's ELF ( E xtenable L oadable F ormat) header, which is usually produced by a compiler. The second one are scripts of any kind.
If a file starts in the very first line with the sequence #! then the next string has to be a pathname of an interpreter. If the kernel reads this line, it calls the interpreter named by this pathname and gives all of the following words in this line as arguments to the interpreter. If there is no file named "something" or "wrong":
#!/bin/bash something wrong echo "This line never gets printed"
bash tries to execute its argument "something wrong" which doesn't exist. The name of the script file is added too. To see this clearly use an echo shebang:
GoalKicker.com – Bash Notes for Professionals 10
Chapter 3: Navigating directories
Section 3.1: Absolute vs relative directories
To change to an absolutely specified directory, use the entire name, starting with a slash /, thus:
cd / home / username / project / abc
If you want to change to a directory near your current on, you can specify a relative location. For example, if you are already in / home / username / project, you can enter the subdirectory abc thus:
cd abc
If you want to go to the directory above the current directory, you can use the alias ... For example, if you were in / home / username / project / abc and wanted to go to / home / username / project, then you would do the following:
cd ..
This may also be called going "up" a directory.
Section 3.2: Change to the last directory
For the current shell, this takes you to the previous directory that you were in, no matter where it was.
cd -
Doing it multiple times effectively "toggles" you being in the current directory or the previous one.
Section 3.3: Change to the home directory
The default directory is the home directory ($HOME, typically / home / username), so cd without any directory takes you there
cd
Or you could be more explicit:
cd $HOME
A shortcut for the home directory is ~, so that could be used as well.
cd ~
Section 3.4: Change to the Directory of the Script
In general, there are two types of Bash scripts :
For the second type of scripts, it is useful to change to the directory where the script is stored. This can be done with the following command:
GoalKicker.com – Bash Notes for Professionals 11
cd "$(dirname "$(readlink -f "$0")" ) "
This command runs 3 commands: