diff options
Diffstat (limited to 'bash.html.markdown')
-rw-r--r-- | bash.html.markdown | 45 |
1 files changed, 35 insertions, 10 deletions
diff --git a/bash.html.markdown b/bash.html.markdown index 856db706..4ed638e6 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -17,22 +17,23 @@ contributors: - ["John Detter", "https://github.com/jdetter"] - ["Harry Mumford-Turner", "https://github.com/harrymt"] - ["Martin Nicholson", "https://github.com/mn113"] + - ["Mark Grimwood", "https://github.com/MarkGrimwood"] filename: LearnBash.sh translators: - ["Dimitri Kokkonis", "https://github.com/kokkonisd"] --- Bash is a name of the unix shell, which was also distributed as the shell -for the GNU operating system and as default shell on Linux and Mac OS X. +for the GNU operating system and as the default shell on most Linux distros. Nearly all examples below can be a part of a shell script or executed directly in the shell. -[Read more here.](http://www.gnu.org/software/bash/manual/bashref.html) +[Read more here.](https://www.gnu.org/software/bash/manual/bashref.html) ```bash #!/usr/bin/env bash # First line of the script is the shebang which tells the system how to execute -# the script: http://en.wikipedia.org/wiki/Shebang_(Unix) +# the script: https://en.wikipedia.org/wiki/Shebang_(Unix) # As you already figured, comments start with #. Shebang is also a comment. # Simple hello world example: @@ -88,6 +89,11 @@ echo ${Variable: -5} # => tring # String length echo ${#Variable} # => 11 +# Indirect expansion +OtherVariable="Variable" +echo ${!OtherVariable} # => Some String +# This will expand the value of OtherVariable + # Default value for variable echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"} # => DefaultValueIfFooIsMissingOrEmpty @@ -193,7 +199,7 @@ then fi # Note that =~ only works within double [[ ]] square brackets, # which are subtly different from single [ ]. -# See http://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs for more on this. +# See https://www.gnu.org/software/bash/manual/bashref.html#Conditional-Constructs for more on this. # Redefine command `ping` as alias to send only 5 packets alias ping='ping -c 5' @@ -225,7 +231,9 @@ cat file.txt # We can also read the file using `cat`: Contents=$(cat file.txt) -echo "START OF FILE\n$Contents\nEND OF FILE" # "\n" prints a new line character +# "\n" prints a new line character +# "-e" to interpret the newline escape characters as escape characters +echo -e "START OF FILE\n$Contents\nEND OF FILE" # => START OF FILE # => [contents of file.txt] # => END OF FILE @@ -318,6 +326,9 @@ echo "#helloworld" | tee output.out >/dev/null # WARNING: `rm` commands cannot be undone rm -v output.out error.err output-and-error.log rm -r tempDir/ # recursively delete +# You can install the `trash-cli` Python package to have `trash` +# which puts files in the system trash and doesn't delete them directly +# see https://pypi.org/project/trash-cli/ if you want to be careful # Commands can be substituted within other commands using $( ): # The following command displays the number of files and directories in the @@ -325,15 +336,15 @@ rm -r tempDir/ # recursively delete echo "There are $(ls | wc -l) items here." # The same can be done using backticks `` but they can't be nested - -#the preferred way is to use $( ). +# the preferred way is to use $( ). echo "There are `ls | wc -l` items here." # Bash uses a `case` statement that works similarly to switch in Java and C++: case "$Variable" in - #List patterns for the conditions you want to meet + # List patterns for the conditions you want to meet 0) echo "There is a zero.";; 1) echo "There is a one.";; - *) echo "It is not null.";; + *) echo "It is not null.";; # match everything esac # `for` loops iterate for as many arguments given: @@ -370,6 +381,13 @@ do cat "$Output" done +# Bash can also accept patterns, like this to `cat` +# all the Markdown files in current directory +for Output in ./*.markdown +do + cat "$Output" +done + # while loop: while [ true ] do @@ -385,13 +403,17 @@ function foo () echo "Arguments work just like script arguments: $@" echo "And: $1 $2..." echo "This is a function" - return 0 + returnValue=0 # Variable values can be returned + return $returnValue } # Call the function `foo` with two arguments, arg1 and arg2: foo arg1 arg2 # => Arguments work just like script arguments: arg1 arg2 # => And: arg1 arg2... # => This is a function +# Return values can be obtained with $? +resultValue=$? +# More than 9 arguments are also possible by using braces, e.g. ${10}, ${11}, ... # or simply bar () @@ -424,6 +446,8 @@ cut -d ',' -f 1 file.txt # replaces every occurrence of 'okay' with 'great' in file.txt # (regex compatible) sed -i 's/okay/great/g' file.txt +# be aware that this -i flag means that file.txt will be changed +# -i or --in-place erase the input file (use --in-place=.backup to keep a back-up) # print to stdout all lines of file.txt which match some regex # The example prints lines which begin with "foo" and end in "bar" @@ -441,7 +465,7 @@ grep -rI "^foo.*bar$" someDir/ # recursively `grep`, but ignore binary files grep "^foo.*bar$" file.txt | grep -v "baz" # if you literally want to search for the string, -# and not the regex, use fgrep (or grep -F) +# and not the regex, use `fgrep` (or `grep -F`) fgrep "foobar" file.txt # The `trap` command allows you to execute a command whenever your script @@ -450,6 +474,7 @@ fgrep "foobar" file.txt trap "rm $TEMP_FILE; exit" SIGHUP SIGINT SIGTERM # `sudo` is used to perform commands as the superuser +# usually it will ask interactively the password of superuser NAME1=$(whoami) NAME2=$(sudo whoami) echo "Was $NAME1, then became more powerful $NAME2" |