From 3a88d4f2443c91dba5a968e38ede2b4f145f152a Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sun, 18 Aug 2013 00:59:35 +0200 Subject: Stub bash file --- bash.html.markdown | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 bash.html.markdown (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown new file mode 100644 index 00000000..ea0a28da --- /dev/null +++ b/bash.html.markdown @@ -0,0 +1,44 @@ +--- + +language: bash +contributors: + - ["Max Yankov", "https://github.com/golergka"] +filename: LearnBash.sh + +--- + +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. +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) + +```bash +#!/bin/sh +# First line of the script is shebang which tells the system how to execute the script: http://en.wikipedia.org/wiki/Shebang_(Unix) +# As you already figured, comments start with #. Shebang is also a comment. + +# Simple hello world example: +echo 'Hello, world!' + +# Each command starts on a new line, or after semicolon: +echo 'This is the first line'; echo 'This is the second line' + +# Declaring a variable looks like this: +VARIABLE="Some string" + +# But not like this: +VARIABLE = "Some string" # Bash will decide that VARIABLE is a command he must execute and give an error because it couldn't be found. + +# Using the variable: +echo $VARIABLE +echo "$VARIABLE" + +# We have the usual if structure: +if true +then + echo "This is expected" +else + echo "And is was not" +fi + +``` \ No newline at end of file -- cgit v1.2.3 From 01f1419dd7d0fba8735cbafb4cff871e52604b07 Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sun, 18 Aug 2013 01:14:00 +0200 Subject: Bash: user input and expressions --- bash.html.markdown | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index ea0a28da..1ddacc33 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -18,7 +18,7 @@ Nearly all examples below can be a part of a shell script or executed directly i # As you already figured, comments start with #. Shebang is also a comment. # Simple hello world example: -echo 'Hello, world!' +echo Hello, world! # Each command starts on a new line, or after semicolon: echo 'This is the first line'; echo 'This is the second line' @@ -32,6 +32,12 @@ VARIABLE = "Some string" # Bash will decide that VARIABLE is a command he must e # Using the variable: echo $VARIABLE echo "$VARIABLE" +# When you use the variable itself — assign it, export it, or else — you write it's name without $. If you want to use variable's value, you should use $. + +# Reading a value from input: +echo "What's your name?" +read NAME # Note that we didn't need to declare new variable +echo Hello, $NAME! # We have the usual if structure: if true @@ -41,4 +47,7 @@ else echo "And is was not" fi +# Expressions are denoted with the following format: +echo $(( 10 + 5 )) + ``` \ No newline at end of file -- cgit v1.2.3 From a538c52fb444fc14782ceb8353f69da04d232e60 Mon Sep 17 00:00:00 2001 From: Darren Lin Date: Sat, 17 Aug 2013 17:33:32 -0700 Subject: expanded the bash tutorial --- bash.html.markdown | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 1ddacc33..4e1eff9e 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -2,7 +2,7 @@ language: bash contributors: - - ["Max Yankov", "https://github.com/golergka"] + - ["Max Yankov", "https://github.com/golergka" - "Darren Lin", "https://github.com/CogBear"] filename: LearnBash.sh --- @@ -50,4 +50,24 @@ fi # Expressions are denoted with the following format: echo $(( 10 + 5 )) -``` \ No newline at end of file +# Commands can be substitued within other commands using $( ): +# The following command displays the number of files and directories in the current directory. +echo "There are $(ls | wc -l) items here." + +#Bash uses a case statement that works similarily to switch in Java and C++: +case "$VARIABLE" +in + #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." +esac + +#For loops iterate for as many arguments given: +#The contents of var $VARIABLE is printed three times. +for $VARIABLE in x y z +do + echo "$VARIABLE" +done + +``` -- cgit v1.2.3 From 3e8c292a10eabd9816f7b0ccb9249661fbb4c3be Mon Sep 17 00:00:00 2001 From: Max Yankov Date: Sun, 18 Aug 2013 14:25:20 +0200 Subject: Bash: commands, attributes, ls, grep & pipe --- bash.html.markdown | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 4e1eff9e..8cf7be18 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -44,12 +44,23 @@ if true then echo "This is expected" else - echo "And is was not" + echo "And this is not" fi # Expressions are denoted with the following format: echo $(( 10 + 5 )) +# Unlike other programming languages, bash is a shell — so it works in a context of current directory. +# You can list files and directories in the current directories with ls command: +ls + +# These commands have options that control their execution: +ls -l # Lists every file and directory on a separate line + +# Results of the previous command can be passed to the next command as input. +# grep command filters the input with provided patterns. That's how we can list txt files in the current directory: +ls -l | grep "\.txt" + # Commands can be substitued within other commands using $( ): # The following command displays the number of files and directories in the current directory. echo "There are $(ls | wc -l) items here." -- cgit v1.2.3 From f33dea8b83bf64ecde36337a5e02cae77f5210de Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 19 Aug 2013 09:14:02 -0700 Subject: Updates --- bash.html.markdown | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 8cf7be18..7421f880 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -1,8 +1,10 @@ --- -language: bash +category: tool +tool: bash contributors: - - ["Max Yankov", "https://github.com/golergka" - "Darren Lin", "https://github.com/CogBear"] + - ["Max Yankov", "https://github.com/golergka"] + - ["Darren Lin", "https://github.com/CogBear"] filename: LearnBash.sh --- @@ -14,7 +16,8 @@ Nearly all examples below can be a part of a shell script or executed directly i ```bash #!/bin/sh -# First line of the script is shebang which tells the system how to execute the script: http://en.wikipedia.org/wiki/Shebang_(Unix) +# First line of the script is shebang which tells the system how to execute +# the script: http://en.wikipedia.org/wiki/Shebang_(Unix) # As you already figured, comments start with #. Shebang is also a comment. # Simple hello world example: @@ -27,12 +30,15 @@ echo 'This is the first line'; echo 'This is the second line' VARIABLE="Some string" # But not like this: -VARIABLE = "Some string" # Bash will decide that VARIABLE is a command he must execute and give an error because it couldn't be found. +VARIABLE = "Some string" +# Bash will decide that VARIABLE is a command it must execute and give an error +# because it couldn't be found. # Using the variable: echo $VARIABLE echo "$VARIABLE" -# When you use the variable itself — assign it, export it, or else — you write it's name without $. If you want to use variable's value, you should use $. +# When you use the variable itself — assign it, export it, or else — you write +# its name without $. If you want to use variable's value, you should use $. # Reading a value from input: echo "What's your name?" @@ -42,43 +48,46 @@ echo Hello, $NAME! # We have the usual if structure: if true then - echo "This is expected" + echo "This is expected" else - echo "And this is not" + echo "And this is not" fi # Expressions are denoted with the following format: echo $(( 10 + 5 )) -# Unlike other programming languages, bash is a shell — so it works in a context of current directory. -# You can list files and directories in the current directories with ls command: +# Unlike other programming languages, bash is a shell — so it works in a context +# of current directory. You can list files and directories in the current +# directories with ls command: ls # These commands have options that control their execution: ls -l # Lists every file and directory on a separate line # Results of the previous command can be passed to the next command as input. -# grep command filters the input with provided patterns. That's how we can list txt files in the current directory: +# grep command filters the input with provided patterns. That's how we can list +# txt files in the current directory: ls -l | grep "\.txt" # Commands can be substitued within other commands using $( ): -# The following command displays the number of files and directories in the current directory. +# The following command displays the number of files and directories in the +# current directory. echo "There are $(ls | wc -l) items here." -#Bash uses a case statement that works similarily to switch in Java and C++: +# Bash uses a case statement that works similarily to switch in Java and C++: case "$VARIABLE" in - #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." + #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." esac #For loops iterate for as many arguments given: #The contents of var $VARIABLE is printed three times. for $VARIABLE in x y z do - echo "$VARIABLE" + echo "$VARIABLE" done ``` -- cgit v1.2.3 From 1a8b22cb4f51b6302e1411ecd94851270fd42eaf Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Thu, 22 Aug 2013 15:26:26 -0500 Subject: Add while loop to bash --- bash.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 7421f880..1473e669 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -53,6 +53,13 @@ else echo "And this is not" fi +# And the usual while loop: +while [true] +do + echo "put loop content here..." + break +done + # Expressions are denoted with the following format: echo $(( 10 + 5 )) -- cgit v1.2.3 From c7a1136772588e57a3f9c20be9102a80a6f7f3e8 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 27 Aug 2013 21:23:09 -0700 Subject: Edits --- bash.html.markdown | 2 -- 1 file changed, 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 7421f880..a0c43c12 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -1,12 +1,10 @@ --- - category: tool tool: bash contributors: - ["Max Yankov", "https://github.com/golergka"] - ["Darren Lin", "https://github.com/CogBear"] filename: LearnBash.sh - --- 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. -- cgit v1.2.3 From e2cd18ba463279a650c2393f68f37131a52b4c4d Mon Sep 17 00:00:00 2001 From: Avjinder Date: Sat, 31 Aug 2013 17:05:04 +0530 Subject: Update bash.html.markdown In the case statement, the "in" keyword should be on the same line as case $VARIABLE. Also, ;; should be present at the end of each command. Shell executes all statements up to the two semicolons that are next to each other. --- bash.html.markdown | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index a0c43c12..76c794c6 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -73,12 +73,11 @@ ls -l | grep "\.txt" echo "There are $(ls | wc -l) items here." # Bash uses a case statement that works similarily to switch in Java and C++: -case "$VARIABLE" -in +case "$VARIABLE" in #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." + 0) echo "There is a zero.";; + 1) echo "There is a one.";; + *) echo "It is not null.";; esac #For loops iterate for as many arguments given: -- cgit v1.2.3 From 1528dd4fbe680f26646be3af17d9fb5dce209dd0 Mon Sep 17 00:00:00 2001 From: JohnYangSam Date: Thu, 12 Sep 2013 02:01:52 -0700 Subject: Correct bash for...in loop example Removed the $ in the variable declaration of the for...in bash loop to correct the code. --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 76c794c6..708131bd 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -82,7 +82,7 @@ esac #For loops iterate for as many arguments given: #The contents of var $VARIABLE is printed three times. -for $VARIABLE in x y z +for VARIABLE in x y z do echo "$VARIABLE" done -- cgit v1.2.3 From de5a359faadffadbff1ccb19ad584ffb1c906796 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Thu, 19 Sep 2013 16:48:09 -0500 Subject: Edit wording of while loop in bash. --- bash.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 1473e669..2faa4988 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -53,10 +53,10 @@ else echo "And this is not" fi -# And the usual while loop: +# while loop: while [true] do - echo "put loop content here..." + echo "loop body here..." break done -- cgit v1.2.3 From c14586e8c3c285b34c20358fa97e44aeed356dde Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 01:06:35 -0300 Subject: add conditional execution info to bash --- bash.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 708131bd..41d0669e 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -4,6 +4,7 @@ tool: bash contributors: - ["Max Yankov", "https://github.com/golergka"] - ["Darren Lin", "https://github.com/CogBear"] + - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"] filename: LearnBash.sh --- @@ -51,6 +52,10 @@ else echo "And this is not" fi +# There is also conditional execution +echo "Always executed" || echo "Only executed if first command fail" +echo "Always executed" && echo "Only executed if first command does NOT fail" + # Expressions are denoted with the following format: echo $(( 10 + 5 )) -- cgit v1.2.3 From 9d0f731ad5c6e3e9ccda43651eb4f445fc8234f0 Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 01:29:05 -0300 Subject: Improve bash variable info --- bash.html.markdown | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 41d0669e..81565b6d 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -36,8 +36,22 @@ VARIABLE = "Some string" # Using the variable: echo $VARIABLE echo "$VARIABLE" +echo '$VARIABLE' # When you use the variable itself — assign it, export it, or else — you write # its name without $. If you want to use variable's value, you should use $. +# Note that ' (single quote) won't expand the variables! + +# String substitution in variables +echo ${VARIABLE/Some/A} +# This will substitute the first occurance of "Some" with "A" + +# Bultin variables: +# There are some useful builtin variables, like +echo "Last program return value: $?" +echo "Script's PID: $$" +echo "Number of arguments: $#" +echo "Scripts arguments: $@" +echo "Scripts arguments separeted in different variables: $1 $2..." # Reading a value from input: echo "What's your name?" -- cgit v1.2.3 From 9c8c0af0af1caef57f463c7c41e56ccc76f414d6 Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 11:04:24 -0300 Subject: add information about input, output, and error redirection --- bash.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 81565b6d..f281c1eb 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -86,6 +86,13 @@ ls -l # Lists every file and directory on a separate line # txt files in the current directory: ls -l | grep "\.txt" +# You can also redirect a command output, input and error output. +python2 hello.py < "input.in" +python2 hello.py > "output.out" +python2 hello.py 2> "error.err" +# The output error will overwrite the file if it exists, if you want to +# concatenate them, use ">>" instead. + # Commands can be substitued within other commands using $( ): # The following command displays the number of files and directories in the # current directory. -- cgit v1.2.3 From 96055ac7a513fef69fd4323df465537b8df835c4 Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 11:09:28 -0300 Subject: better for description --- bash.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index f281c1eb..fe17e710 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -106,9 +106,10 @@ case "$VARIABLE" in *) echo "It is not null.";; esac -#For loops iterate for as many arguments given: -#The contents of var $VARIABLE is printed three times. -for VARIABLE in x y z +# For loops iterate for as many arguments given: +# The contents of var $VARIABLE is printed three times. +# Note that ` ` is equivalent to $( ) and that seq returns a sequence of size 3. +for VARIABLE in `seq 3` do echo "$VARIABLE" done -- cgit v1.2.3 From e1c34ca138cc1a978a85ba5062bed47219ac2d1c Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 11:20:10 -0300 Subject: improve if on bash --- bash.html.markdown | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index fe17e710..4a290358 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -59,11 +59,12 @@ read NAME # Note that we didn't need to declare new variable echo Hello, $NAME! # We have the usual if structure: -if true +# use 'man test' for more info about conditionals +if [ $NAME -ne $USER ] then - echo "This is expected" + echo "Your name is you username" else - echo "And this is not" + echo "Your name isn't you username" fi # There is also conditional execution -- cgit v1.2.3 From 182eab60517e571931d7b928c28fb88a88fca894 Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 11:28:59 -0300 Subject: add function information for bash --- bash.html.markdown | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 4a290358..ed93d58b 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -115,4 +115,16 @@ do echo "$VARIABLE" done +# You can also define functions +# Definition: +foo () +{ + echo "Arguments work just like script arguments: $@" + echo "And: $1 $2..." + echo "This is a function" + return 0 +} + +# Calling your function +foo "My name is" $NAME ``` -- cgit v1.2.3 From b3288dc9cd0e1bd39e0e6d41664b120ea2f2e02a Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 22 Sep 2013 11:35:04 -0300 Subject: add useful commands --- bash.html.markdown | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index ed93d58b..4d80545e 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -127,4 +127,16 @@ foo () # Calling your function foo "My name is" $NAME + +# There are a lot of useful commands you should learn: +tail -n 10 file.txt +# prints last 10 lines of file.txt +head -n 10 file.txt +# prints first 10 lines of file.txt +sort file.txt +# sort file.txt's lines +uniq -d file.txt +# report or omit repeated lines, with -d it reports them +cut -d ',' -f 1 file.txt +# prints only the first column before the ',' character ``` -- cgit v1.2.3 From b2028c7f0c3087b34b601ab27e61cc59ae1e9a21 Mon Sep 17 00:00:00 2001 From: Christos Kontas Date: Fri, 4 Oct 2013 18:32:11 +0300 Subject: [bash] Fix some spell errors in comments --- bash.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 4d80545e..276bc31f 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -94,12 +94,12 @@ python2 hello.py 2> "error.err" # The output error will overwrite the file if it exists, if you want to # concatenate them, use ">>" instead. -# Commands can be substitued within other commands using $( ): +# Commands can be substituted within other commands using $( ): # The following command displays the number of files and directories in the # current directory. echo "There are $(ls | wc -l) items here." -# Bash uses a case statement that works similarily to switch in Java and C++: +# 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 0) echo "There is a zero.";; -- cgit v1.2.3 From 3bfc820721618af1c1421e2858198bbc739ec70a Mon Sep 17 00:00:00 2001 From: alexandre medeiros Date: Sun, 13 Oct 2013 00:14:24 -0300 Subject: add another way to define functions to bash (fix #380) --- bash.html.markdown | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 276bc31f..d208b957 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -117,7 +117,7 @@ done # You can also define functions # Definition: -foo () +function foo () { echo "Arguments work just like script arguments: $@" echo "And: $1 $2..." @@ -125,6 +125,13 @@ foo () return 0 } +# or simply +bar () +{ + echo "Another way to declare functions!" + return 0 +} + # Calling your function foo "My name is" $NAME -- cgit v1.2.3 From 248284c91b641f2bd635dfe5a640f1fd3b8cd6a3 Mon Sep 17 00:00:00 2001 From: jakub-g Date: Mon, 9 Dec 2013 13:17:20 +0100 Subject: [bash] Replace `seq` with `{1..3}`, explain backticks `seq` is not standard, it's not available in MINGW and others. Using backticks is not the recommended way - e.g. they can't be nested. --- bash.html.markdown | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index afc46eb0..1f1c32c0 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -99,6 +99,10 @@ python2 hello.py 2> "error.err" # current directory. 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 $( ). +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 @@ -109,8 +113,7 @@ esac # For loops iterate for as many arguments given: # The contents of var $VARIABLE is printed three times. -# Note that ` ` is equivalent to $( ) and that seq returns a sequence of size 3. -for VARIABLE in `seq 3` +for VARIABLE in {1..3} do echo "$VARIABLE" done -- cgit v1.2.3 From c9a282d08fb1f8670fa328e0e7cdb8801382b88a Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Wed, 18 Dec 2013 09:37:27 +0100 Subject: Add example for default value --- bash.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 1f1c32c0..815290dd 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -5,6 +5,7 @@ contributors: - ["Max Yankov", "https://github.com/golergka"] - ["Darren Lin", "https://github.com/CogBear"] - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"] + - ["Denis Arh", "https://github.com/darh"] filename: LearnBash.sh --- @@ -45,6 +46,10 @@ echo '$VARIABLE' echo ${VARIABLE/Some/A} # This will substitute the first occurance of "Some" with "A" +# Default value for variable +echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"} +# This works for null (FOO=), empty string (FOO=""), zero (FOO=0) returns 0 + # Bultin variables: # There are some useful builtin variables, like echo "Last program return value: $?" -- cgit v1.2.3 From 5bd86ec047ba12decea1105d47b0496ab7e435cb Mon Sep 17 00:00:00 2001 From: kyr Date: Fri, 27 Dec 2013 16:21:24 +0100 Subject: spelling fixes in bash (en) --- bash.html.markdown | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 815290dd..a6bd2b7c 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -21,7 +21,7 @@ Nearly all examples below can be a part of a shell script or executed directly i # As you already figured, comments start with #. Shebang is also a comment. # Simple hello world example: -echo Hello, world! +echo Hello world! # Each command starts on a new line, or after semicolon: echo 'This is the first line'; echo 'This is the second line' @@ -56,24 +56,24 @@ echo "Last program return value: $?" echo "Script's PID: $$" echo "Number of arguments: $#" echo "Scripts arguments: $@" -echo "Scripts arguments separeted in different variables: $1 $2..." +echo "Scripts arguments seperated in different variables: $1 $2..." # Reading a value from input: echo "What's your name?" -read NAME # Note that we didn't need to declare new variable +read NAME # Note that we didn't need to declare a new variable echo Hello, $NAME! # We have the usual if structure: # use 'man test' for more info about conditionals if [ $NAME -ne $USER ] then - echo "Your name is you username" + echo "Your name is your username" else - echo "Your name isn't you username" + echo "Your name isn't your username" fi # There is also conditional execution -echo "Always executed" || echo "Only executed if first command fail" +echo "Always executed" || echo "Only executed if first command fails" echo "Always executed" && echo "Only executed if first command does NOT fail" # Expressions are denoted with the following format: @@ -81,7 +81,7 @@ echo $(( 10 + 5 )) # Unlike other programming languages, bash is a shell — so it works in a context # of current directory. You can list files and directories in the current -# directories with ls command: +# directory with the ls command: ls # These commands have options that control their execution: @@ -89,10 +89,10 @@ ls -l # Lists every file and directory on a separate line # Results of the previous command can be passed to the next command as input. # grep command filters the input with provided patterns. That's how we can list -# txt files in the current directory: +# .txt files in the current directory: ls -l | grep "\.txt" -# You can also redirect a command output, input and error output. +# You can also redirect a command, input and error output. python2 hello.py < "input.in" python2 hello.py > "output.out" python2 hello.py 2> "error.err" @@ -116,7 +116,7 @@ case "$VARIABLE" in *) echo "It is not null.";; esac -# For loops iterate for as many arguments given: +# for loops iterate for as many arguments given: # The contents of var $VARIABLE is printed three times. for VARIABLE in {1..3} do -- cgit v1.2.3 From a23e5807ad0c241672eeb7eaffea4798f5954d01 Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Wed, 12 Feb 2014 12:02:29 -0800 Subject: Update bash.html.markdown use /bin/bash for bash, not /bin/sh --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index a6bd2b7c..e2c5bbaf 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -15,7 +15,7 @@ Nearly all examples below can be a part of a shell script or executed directly i [Read more here.](http://www.gnu.org/software/bash/manual/bashref.html) ```bash -#!/bin/sh +#!/bin/bash # First line of the script is shebang which tells the system how to execute # the script: http://en.wikipedia.org/wiki/Shebang_(Unix) # As you already figured, comments start with #. Shebang is also a comment. -- cgit v1.2.3 From 03e6892a5f84f063b723d433947929750349d625 Mon Sep 17 00:00:00 2001 From: Brian Stearns Date: Thu, 20 Feb 2014 17:12:12 -0500 Subject: Fixed random spelling error. --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index e2c5bbaf..70a3b52a 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -50,7 +50,7 @@ echo ${VARIABLE/Some/A} echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"} # This works for null (FOO=), empty string (FOO=""), zero (FOO=0) returns 0 -# Bultin variables: +# Builtin variables: # There are some useful builtin variables, like echo "Last program return value: $?" echo "Script's PID: $$" -- cgit v1.2.3 From 1b67b8f2304c1787d14bb26c817774b4b7aa2eac Mon Sep 17 00:00:00 2001 From: Justin Campbell Date: Fri, 28 Feb 2014 11:24:45 -0500 Subject: Variable substr --- bash.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 70a3b52a..d5d08e9d 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -46,6 +46,10 @@ echo '$VARIABLE' echo ${VARIABLE/Some/A} # This will substitute the first occurance of "Some" with "A" +# Substring from a variable +echo ${VARIABLE:0:7} +# This will return only the first 7 characters of the value + # Default value for variable echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"} # This works for null (FOO=), empty string (FOO=""), zero (FOO=0) returns 0 -- cgit v1.2.3 From e859a4edde405fc020cccc04a806ea877e26ed75 Mon Sep 17 00:00:00 2001 From: Akira Hirose Date: Wed, 23 Jul 2014 15:32:50 +0900 Subject: just collected the order of comments and commands of last part --- bash.html.markdown | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index d5d08e9d..29b0c8db 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -155,14 +155,14 @@ bar () foo "My name is" $NAME # There are a lot of useful commands you should learn: -tail -n 10 file.txt # prints last 10 lines of file.txt -head -n 10 file.txt +tail -n 10 file.txt # prints first 10 lines of file.txt -sort file.txt +head -n 10 file.txt # sort file.txt's lines -uniq -d file.txt +sort file.txt # report or omit repeated lines, with -d it reports them -cut -d ',' -f 1 file.txt +uniq -d file.txt # prints only the first column before the ',' character +cut -d ',' -f 1 file.txt ``` -- cgit v1.2.3 From 4f97478d6beba127180f7f6d3278aef79b901441 Mon Sep 17 00:00:00 2001 From: Akira Hirose Date: Wed, 23 Jul 2014 15:43:18 +0900 Subject: add a contributor line :) --- bash.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 29b0c8db..15d1c068 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -6,6 +6,7 @@ contributors: - ["Darren Lin", "https://github.com/CogBear"] - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"] - ["Denis Arh", "https://github.com/darh"] + - ["akirahirose", "https://twitter.com/akirahirose"] filename: LearnBash.sh --- -- cgit v1.2.3 From 3f8dbe8e77ca8b1602a317e218b78df94afd8f66 Mon Sep 17 00:00:00 2001 From: LumenTeun Date: Wed, 6 Aug 2014 19:30:15 +0100 Subject: && and || in Bash if statements. --- bash.html.markdown | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 15d1c068..845ebead 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -7,6 +7,7 @@ contributors: - ["Alexandre Medeiros", "http://alemedeiros.sdf.org"] - ["Denis Arh", "https://github.com/darh"] - ["akirahirose", "https://twitter.com/akirahirose"] + - ["Anton Strömkvist", "http://lutic.org/"] filename: LearnBash.sh --- @@ -81,6 +82,17 @@ fi echo "Always executed" || echo "Only executed if first command fails" echo "Always executed" && echo "Only executed if first command does NOT fail" +# To use && and || with if statements, you need multiple pairs of square brackets: +if [ $NAME == "Steve" ] && [ $AGE -eq 15 ] +then + echo "This will run if $NAME is Steve AND $AGE is 15." +fi + +if [ $NAME == "Daniya" ] || [ $NAME == "Zach" ] +then + echo "This will run if $NAME is Daniya OR Zach." +fi + # Expressions are denoted with the following format: echo $(( 10 + 5 )) -- cgit v1.2.3 From 8be20de321b2331ab717319ca3c57c460a85de7f Mon Sep 17 00:00:00 2001 From: LumenTeun Date: Fri, 8 Aug 2014 18:11:17 +0100 Subject: Fixed while loop. --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 845ebead..96f2414d 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -141,7 +141,7 @@ do done # while loop: -while [true] +while [ true ] do echo "loop body here..." break -- cgit v1.2.3 From 43209662626cdb4ed4b9e656a72c092512c4e9cc Mon Sep 17 00:00:00 2001 From: LumenTeun Date: Fri, 8 Aug 2014 18:24:29 +0100 Subject: Fixed typo. --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 96f2414d..2056a9ea 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -134,7 +134,7 @@ case "$VARIABLE" in esac # for loops iterate for as many arguments given: -# The contents of var $VARIABLE is printed three times. +# The contents of $VARIABLE is printed three times. for VARIABLE in {1..3} do echo "$VARIABLE" -- cgit v1.2.3 From e1365274f7f9217a035b3e078fd7586eeef5f886 Mon Sep 17 00:00:00 2001 From: LumenTeun Date: Fri, 8 Aug 2014 18:24:43 +0100 Subject: Added two new ways to use for loops. --- bash.html.markdown | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 2056a9ea..061d35b0 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -140,6 +140,20 @@ do echo "$VARIABLE" done +# They can also be used to act on files.. +# This will run the command 'cat' on file1 and file2 +for VARIABLE in file1 file2 +do + cat "$VARIABLE" +done + +# ..or the output from a command +# This will cat the output from ls. +for OUTPUT in $(ls) +do + cat "$OUTPUT" +done + # while loop: while [ true ] do -- cgit v1.2.3 From 7bfbec395acc3ddd61b9cc1d7c4b0ee3877f250b Mon Sep 17 00:00:00 2001 From: Samuel Marks Date: Sat, 23 Aug 2014 16:54:13 +1000 Subject: -ne not equal --- bash.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 061d35b0..57fb5c55 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -73,9 +73,9 @@ echo Hello, $NAME! # use 'man test' for more info about conditionals if [ $NAME -ne $USER ] then - echo "Your name is your username" -else echo "Your name isn't your username" +else + echo "Your name is your username" fi # There is also conditional execution -- cgit v1.2.3 From eefa3add633ef1e80fb7403eec7854da1077188b Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 5 Sep 2014 20:51:48 -0500 Subject: Add traditional for loop bash example to close https://github.com/adambard/learnxinyminutes-docs/pull/654 --- bash.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 57fb5c55..dc7d32b6 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -8,6 +8,7 @@ contributors: - ["Denis Arh", "https://github.com/darh"] - ["akirahirose", "https://twitter.com/akirahirose"] - ["Anton Strömkvist", "http://lutic.org/"] + - ["Rahil Momin", "https://github.com/iamrahil"] filename: LearnBash.sh --- @@ -140,6 +141,12 @@ do echo "$VARIABLE" done +# Or write it the "traditional for loop" way: +for ((a=1; a <= 3; a++)) +do + echo $a +done + # They can also be used to act on files.. # This will run the command 'cat' on file1 and file2 for VARIABLE in file1 file2 -- cgit v1.2.3 From b52a32dd2439c293892b72e35b57b4887a5a53cf Mon Sep 17 00:00:00 2001 From: "Gregory S. Kielian" Date: Sat, 27 Sep 2014 12:04:25 -0700 Subject: Added `sed` and `grep` examples to useful-commands --- bash.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index dc7d32b6..0f571e83 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -9,6 +9,7 @@ contributors: - ["akirahirose", "https://twitter.com/akirahirose"] - ["Anton Strömkvist", "http://lutic.org/"] - ["Rahil Momin", "https://github.com/iamrahil"] + - ["Gregrory Kielian", "https://github.com/gskielian"] filename: LearnBash.sh --- @@ -199,4 +200,8 @@ sort file.txt uniq -d file.txt # prints only the first column before the ',' character cut -d ',' -f 1 file.txt +# replaces every occurance of 'apples' with 'oranges' in file.txt +sed -i 's/apples/oranges/g' file.txt +# prints the number of occurances of "foo" in file.txt +grep -c "foo" file.txt ``` -- cgit v1.2.3 From e9c21740df4c93ee4575eca41b0028fa2d90ce1b Mon Sep 17 00:00:00 2001 From: "Gregory S. Kielian" Date: Sat, 27 Sep 2014 12:06:36 -0700 Subject: amended grep description --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 0f571e83..fd347488 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -202,6 +202,6 @@ uniq -d file.txt cut -d ',' -f 1 file.txt # replaces every occurance of 'apples' with 'oranges' in file.txt sed -i 's/apples/oranges/g' file.txt -# prints the number of occurances of "foo" in file.txt +# prints the number of lines with the string "foo" in file.txt grep -c "foo" file.txt ``` -- cgit v1.2.3 From d22d591e3e2120dd1bed70cd8093569c8b7101c2 Mon Sep 17 00:00:00 2001 From: "Gregory S. Kielian" Date: Sat, 27 Sep 2014 12:12:56 -0700 Subject: amended sed and bash descriptions --- bash.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index fd347488..160fe8f2 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -200,8 +200,8 @@ sort file.txt uniq -d file.txt # prints only the first column before the ',' character cut -d ',' -f 1 file.txt -# replaces every occurance of 'apples' with 'oranges' in file.txt +# replaces every occurrence of 'apples' with 'oranges' in file.txt sed -i 's/apples/oranges/g' file.txt -# prints the number of lines with the string "foo" in file.txt +# prints the number of lines containing the string "foo" in file.txt grep -c "foo" file.txt ``` -- cgit v1.2.3 From d83a0f038558cf32680287417bbd692b33fe13cc Mon Sep 17 00:00:00 2001 From: "Gregory S. Kielian" Date: Sat, 4 Oct 2014 14:50:00 -0700 Subject: Changed descriptions, added grep, fgrep examples --- bash.html.markdown | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 160fe8f2..9b199b8c 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -200,8 +200,12 @@ sort file.txt uniq -d file.txt # prints only the first column before the ',' character cut -d ',' -f 1 file.txt -# replaces every occurrence of 'apples' with 'oranges' in file.txt -sed -i 's/apples/oranges/g' file.txt -# prints the number of lines containing the string "foo" in file.txt -grep -c "foo" file.txt +# replaces every occurrence of 'okay' with 'great' in file.txt, (regex compatible) +sed -i 's/okay/great/g' file.txt +# print to stdout all lines of file.txt which match some regex, the example prints lines which beginning with "foo" and end in "bar" +grep "^foo.*bar$" file.txt +# pass the option "-c" to instead print the number of lines matching the regex +grep -c "^foo.*bar$" file.txt +# if you literally want to search for the string, and not the regex, use fgrep (or grep -F) +fgrep "^foo.*bar$" file.txt ``` -- cgit v1.2.3 From 01b671bf398001647650db24dcc51f584ea82490 Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Fri, 17 Oct 2014 20:54:45 -0500 Subject: Fix beginning typo from bash --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 9b199b8c..11c1f3a2 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -202,7 +202,7 @@ uniq -d file.txt 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 -# print to stdout all lines of file.txt which match some regex, the example prints lines which beginning with "foo" and end in "bar" +# print to stdout all lines of file.txt which match some regex, the example prints lines which begin with "foo" and end in "bar" grep "^foo.*bar$" file.txt # pass the option "-c" to instead print the number of lines matching the regex grep -c "^foo.*bar$" file.txt -- cgit v1.2.3 From 112b0b7662d3e235e8bee88ba45fccd8d7932e98 Mon Sep 17 00:00:00 2001 From: Wes Turner Date: Thu, 30 Oct 2014 19:46:37 -0500 Subject: DOC: bash.html.markdown: add bash docs examples --- bash.html.markdown | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 11c1f3a2..81d586c4 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -208,4 +208,30 @@ grep "^foo.*bar$" file.txt grep -c "^foo.*bar$" file.txt # if you literally want to search for the string, and not the regex, use fgrep (or grep -F) fgrep "^foo.*bar$" file.txt + + +# Read Bash shell builtins documentation with the bash 'help' builtin: +help +help help +help for +help return +help source +help . + +# Read Bash manpage documentation with man +apropos bash +man 1 bash +man bash + +# Read info documentation with info (? for help) +apropos info | grep '^info.*(' +man info +info info +info 5 info + +# Read bash info documentation: +info bash +info bash 'Bash Features' +info bash 6 +info --apropos bash ``` -- cgit v1.2.3 From e287075690f73c934825fc413bcc4a3e2be4456d Mon Sep 17 00:00:00 2001 From: Wes Turner Date: Thu, 30 Oct 2014 19:20:05 -0500 Subject: [bash/en] bash.html.markdown: add bash redirection examples --- bash.html.markdown | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 11c1f3a2..b0011420 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -111,12 +111,45 @@ ls -l # Lists every file and directory on a separate line # .txt files in the current directory: ls -l | grep "\.txt" -# You can also redirect a command, input and error output. -python2 hello.py < "input.in" -python2 hello.py > "output.out" -python2 hello.py 2> "error.err" -# The output error will overwrite the file if it exists, if you want to -# concatenate them, use ">>" instead. +# You can redirect command input and output (stdin, stdout, and stderr). +# Read from stdin until ^EOF$ and overwrite hello.py with the lines +# between "EOF": +cat > hello.py << EOF +#!/usr/bin/env python +from __future__ import print_function +import sys +print("#stdout", file=sys.stdout) +print("#stderr", file=sys.stderr) +for line in sys.stdin: + print(line, file=sys.stdout) +EOF + +# Run hello.py with various stdin, stdout, and stderr redirections: +python hello.py < "input.in" +python hello.py > "output.out" +python hello.py 2> "error.err" +python hello.py > "output-and-error.log" 2>&1 +python hello.py > /dev/null 2>&1 +# The output error will overwrite the file if it exists, +# if you want to append instead, use ">>": +python hello.py >> "output.out" 2>> "error.err" + +# Overwrite output.txt, append to error.err, and count lines: +info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err +wc -l output.out error.err + +# Run a command and print its file descriptor (e.g. /dev/fd/123) +# see: man fd +echo <(echo "#helloworld") + +# Overwrite output.txt with "#helloworld": +cat > output.out <(echo "#helloworld") +echo "#helloworld" > output.out +echo "#helloworld" | cat > output.out +echo "#helloworld" | tee output.out >/dev/null + +# Cleanup temporary files verbosely (add '-i' for interactive) +rm -v output.out error.err output-and-error.log # Commands can be substituted within other commands using $( ): # The following command displays the number of files and directories in the -- cgit v1.2.3 From 19f6739cbaa06b6b40b835115a0361f5e77bd0e0 Mon Sep 17 00:00:00 2001 From: Sriram Sundarraj Date: Fri, 24 Apr 2015 02:36:54 +0530 Subject: [bash/en] Fixed overflowing line. --- bash.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 3b163638..35bed9a2 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -235,11 +235,13 @@ uniq -d file.txt 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 -# print to stdout all lines of file.txt which match some regex, the example prints lines which begin with "foo" and end in "bar" +# print to stdout all lines of file.txt which match some regex +# The example prints lines which begin with "foo" and end in "bar" grep "^foo.*bar$" file.txt # pass the option "-c" to instead print the number of lines matching the regex grep -c "^foo.*bar$" file.txt -# if you literally want to search for the string, and not the regex, use fgrep (or grep -F) +# if you literally want to search for the string, +# and not the regex, use fgrep (or grep -F) fgrep "^foo.*bar$" file.txt -- cgit v1.2.3 From 9d3462953604d0d331191bb1fca82295e1ece6ed Mon Sep 17 00:00:00 2001 From: Etan Reisner Date: Sun, 26 Apr 2015 13:22:48 -0400 Subject: Add myself as a contributor. --- bash.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 35bed9a2..b9cd53ff 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -10,6 +10,7 @@ contributors: - ["Anton Strömkvist", "http://lutic.org/"] - ["Rahil Momin", "https://github.com/iamrahil"] - ["Gregrory Kielian", "https://github.com/gskielian"] + - ["Etan Reisner", "https://github.com/deryni"] filename: LearnBash.sh --- -- cgit v1.2.3 From e2ca2c9550474ca3dad0f7ef8735e614da081258 Mon Sep 17 00:00:00 2001 From: Etan Reisner Date: Sun, 26 Apr 2015 13:32:41 -0400 Subject: Add another very common shell variable assignment mistake. --- bash.html.markdown | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index b9cd53ff..77ee37f6 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -37,7 +37,14 @@ VARIABLE="Some string" # But not like this: VARIABLE = "Some string" # Bash will decide that VARIABLE is a command it must execute and give an error -# because it couldn't be found. +# because it can't be found. + +# Or like this: +Variable= 'Some string' +# Bash will decide that 'Some string' is a command it must execute and give an +# error because it can't be found. (In this case the 'Variable=' part is seen +# as a variable assignment valid only for the scope of the 'Some string' +# command.) # Using the variable: echo $VARIABLE -- cgit v1.2.3 From aa11cc659de990a6c4d4104bcc733f373b079ae7 Mon Sep 17 00:00:00 2001 From: Etan Reisner Date: Mon, 27 Apr 2015 07:30:17 -0400 Subject: Follow variable capitalization on this branch. --- bash.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 77ee37f6..e0c12f97 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -40,9 +40,9 @@ VARIABLE = "Some string" # because it can't be found. # Or like this: -Variable= 'Some string' +VARIABLE= 'Some string' # Bash will decide that 'Some string' is a command it must execute and give an -# error because it can't be found. (In this case the 'Variable=' part is seen +# error because it can't be found. (In this case the 'VARIABLE=' part is seen # as a variable assignment valid only for the scope of the 'Some string' # command.) -- cgit v1.2.3 From 8758bb845b063b76b55d2c178d0dc393b2a39c5c Mon Sep 17 00:00:00 2001 From: Andrey Samsonov Date: Sat, 2 May 2015 15:34:56 +0400 Subject: Clear that length and position can be set by variable in substring extraction --- bash.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index e0c12f97..81f85d28 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -59,7 +59,8 @@ echo ${VARIABLE/Some/A} # This will substitute the first occurance of "Some" with "A" # Substring from a variable -echo ${VARIABLE:0:7} +LENGTH=7 +echo ${VARIABLE:0:LENGTH} # This will return only the first 7 characters of the value # Default value for variable -- cgit v1.2.3 From 6848f45ebeee759afdaa34e6cdf367fe31bf1bab Mon Sep 17 00:00:00 2001 From: Andrey Samsonov Date: Sat, 2 May 2015 16:21:03 +0400 Subject: Clear explanation of default value expression --- bash.html.markdown | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index e0c12f97..58dc3003 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -64,7 +64,8 @@ echo ${VARIABLE:0:7} # Default value for variable echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"} -# This works for null (FOO=), empty string (FOO=""), zero (FOO=0) returns 0 +# This works for null (FOO=) and empty string (FOO=""); zero (FOO=0) returns 0. +# Note that it only returns default value and doesn't change variable value. # Builtin variables: # There are some useful builtin variables, like -- cgit v1.2.3 From 8b7a2fff9a71b8fa8754947434b8b1f184ed2de1 Mon Sep 17 00:00:00 2001 From: Etan Reisner Date: Wed, 29 Apr 2015 22:11:20 -0400 Subject: Don't use ALL_CAPS variable names. ALL_CAPS variable names are traditionally "reserved" for use by the shell/system. (People often try to use PATH for things locally and then wonder why their commands all stop working for example. --- bash.html.markdown | 58 +++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 4c50c653..937d2c96 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -32,40 +32,40 @@ echo Hello world! echo 'This is the first line'; echo 'This is the second line' # Declaring a variable looks like this: -VARIABLE="Some string" +Variable="Some string" # But not like this: -VARIABLE = "Some string" -# Bash will decide that VARIABLE is a command it must execute and give an error +Variable = "Some string" +# Bash will decide that Variable is a command it must execute and give an error # because it can't be found. # Or like this: -VARIABLE= 'Some string' +Variable= 'Some string' # Bash will decide that 'Some string' is a command it must execute and give an -# error because it can't be found. (In this case the 'VARIABLE=' part is seen +# error because it can't be found. (In this case the 'Variable=' part is seen # as a variable assignment valid only for the scope of the 'Some string' # command.) # Using the variable: -echo $VARIABLE -echo "$VARIABLE" -echo '$VARIABLE' +echo $Variable +echo "$Variable" +echo '$Variable' # When you use the variable itself — assign it, export it, or else — you write # its name without $. If you want to use variable's value, you should use $. # Note that ' (single quote) won't expand the variables! # String substitution in variables -echo ${VARIABLE/Some/A} +echo ${Variable/Some/A} # This will substitute the first occurance of "Some" with "A" # Substring from a variable -LENGTH=7 -echo ${VARIABLE:0:LENGTH} +Length=7 +echo ${Variable:0:Length} # This will return only the first 7 characters of the value # Default value for variable -echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"} -# This works for null (FOO=) and empty string (FOO=""); zero (FOO=0) returns 0. +echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"} +# This works for null (Foo=) and empty string (Foo=""); zero (Foo=0) returns 0. # Note that it only returns default value and doesn't change variable value. # Builtin variables: @@ -78,12 +78,12 @@ echo "Scripts arguments seperated in different variables: $1 $2..." # Reading a value from input: echo "What's your name?" -read NAME # Note that we didn't need to declare a new variable -echo Hello, $NAME! +read Name # Note that we didn't need to declare a new variable +echo Hello, $Name! # We have the usual if structure: # use 'man test' for more info about conditionals -if [ $NAME -ne $USER ] +if [ $Name -ne $USER ] then echo "Your name isn't your username" else @@ -95,14 +95,14 @@ echo "Always executed" || echo "Only executed if first command fails" echo "Always executed" && echo "Only executed if first command does NOT fail" # To use && and || with if statements, you need multiple pairs of square brackets: -if [ $NAME == "Steve" ] && [ $AGE -eq 15 ] +if [ $Name == "Steve" ] && [ $Age -eq 15 ] then - echo "This will run if $NAME is Steve AND $AGE is 15." + echo "This will run if $Name is Steve AND $Age is 15." fi -if [ $NAME == "Daniya" ] || [ $NAME == "Zach" ] +if [ $Name == "Daniya" ] || [ $Name == "Zach" ] then - echo "This will run if $NAME is Daniya OR Zach." + echo "This will run if $Name is Daniya OR Zach." fi # Expressions are denoted with the following format: @@ -171,7 +171,7 @@ echo "There are $(ls | wc -l) items here." 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 +case "$Variable" in #List patterns for the conditions you want to meet 0) echo "There is a zero.";; 1) echo "There is a one.";; @@ -179,10 +179,10 @@ case "$VARIABLE" in esac # for loops iterate for as many arguments given: -# The contents of $VARIABLE is printed three times. -for VARIABLE in {1..3} +# The contents of $Variable is printed three times. +for Variable in {1..3} do - echo "$VARIABLE" + echo "$Variable" done # Or write it the "traditional for loop" way: @@ -193,16 +193,16 @@ done # They can also be used to act on files.. # This will run the command 'cat' on file1 and file2 -for VARIABLE in file1 file2 +for Variable in file1 file2 do - cat "$VARIABLE" + cat "$Variable" done # ..or the output from a command # This will cat the output from ls. -for OUTPUT in $(ls) +for Output in $(ls) do - cat "$OUTPUT" + cat "$Output" done # while loop: @@ -230,7 +230,7 @@ bar () } # Calling your function -foo "My name is" $NAME +foo "My name is" $Name # There are a lot of useful commands you should learn: # prints last 10 lines of file.txt -- cgit v1.2.3 From e4c261567533921f35ce4e65ebfe6621a128992b Mon Sep 17 00:00:00 2001 From: Levi Bostian Date: Mon, 11 May 2015 21:20:02 -0500 Subject: Fix issue with referring to "output.txt" but examples use "output.out" Fixes https://github.com/adambard/learnxinyminutes-docs/issues/1095 --- bash.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 937d2c96..ee783c14 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -144,7 +144,7 @@ python hello.py > /dev/null 2>&1 # if you want to append instead, use ">>": python hello.py >> "output.out" 2>> "error.err" -# Overwrite output.txt, append to error.err, and count lines: +# Overwrite output.out, append to error.err, and count lines: info bash 'Basic Shell Features' 'Redirections' > output.out 2>> error.err wc -l output.out error.err @@ -152,7 +152,7 @@ wc -l output.out error.err # see: man fd echo <(echo "#helloworld") -# Overwrite output.txt with "#helloworld": +# Overwrite output.out with "#helloworld": cat > output.out <(echo "#helloworld") echo "#helloworld" > output.out echo "#helloworld" | cat > output.out -- cgit v1.2.3 From 8909457ae46dc8fb151ef146acb3f6b8402f3407 Mon Sep 17 00:00:00 2001 From: Liam Edwards-Playne Date: Wed, 20 May 2015 11:23:55 +1000 Subject: fix spelling errors --- bash.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index ee783c14..c2312d7d 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -56,7 +56,7 @@ echo '$Variable' # String substitution in variables echo ${Variable/Some/A} -# This will substitute the first occurance of "Some" with "A" +# This will substitute the first occurence of "Some" with "A" # Substring from a variable Length=7 @@ -74,7 +74,7 @@ echo "Last program return value: $?" echo "Script's PID: $$" echo "Number of arguments: $#" echo "Scripts arguments: $@" -echo "Scripts arguments seperated in different variables: $1 $2..." +echo "Scripts arguments separated in different variables: $1 $2..." # Reading a value from input: echo "What's your name?" -- cgit v1.2.3 From de676b62b83fcaaa9977cca9adb9c38383b64f35 Mon Sep 17 00:00:00 2001 From: Liam Edwards-Playne Date: Tue, 26 May 2015 11:25:07 +1000 Subject: fixed spelling error --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index c2312d7d..08182c2c 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -56,7 +56,7 @@ echo '$Variable' # String substitution in variables echo ${Variable/Some/A} -# This will substitute the first occurence of "Some" with "A" +# This will substitute the first occurrence of "Some" with "A" # Substring from a variable Length=7 -- cgit v1.2.3 From acc9a73c018a28a9c8ead7b108dd1fdfee7a797b Mon Sep 17 00:00:00 2001 From: ksami Date: Fri, 2 Oct 2015 22:08:27 +0800 Subject: [bash/en] Improved descriptions --- bash.html.markdown | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index 08182c2c..d4f3d424 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -51,7 +51,7 @@ echo $Variable echo "$Variable" echo '$Variable' # When you use the variable itself — assign it, export it, or else — you write -# its name without $. If you want to use variable's value, you should use $. +# its name without $. If you want to use the variable's value, you should use $. # Note that ' (single quote) won't expand the variables! # String substitution in variables @@ -70,11 +70,11 @@ echo ${Foo:-"DefaultValueIfFooIsMissingOrEmpty"} # Builtin variables: # There are some useful builtin variables, like -echo "Last program return value: $?" +echo "Last program's return value: $?" echo "Script's PID: $$" -echo "Number of arguments: $#" -echo "Scripts arguments: $@" -echo "Scripts arguments separated in different variables: $1 $2..." +echo "Number of arguments passed to script: $#" +echo "All arguments passed to script: $@" +echo "Script's arguments separated into different variables: $1 $2..." # Reading a value from input: echo "What's your name?" @@ -108,8 +108,8 @@ fi # Expressions are denoted with the following format: echo $(( 10 + 5 )) -# Unlike other programming languages, bash is a shell — so it works in a context -# of current directory. You can list files and directories in the current +# Unlike other programming languages, bash is a shell so it works in the context +# of a current directory. You can list files and directories in the current # directory with the ls command: ls -- cgit v1.2.3 From 960ee4a1856db8eadb96277bb2422edfa8f2a81c Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 23:11:24 -0400 Subject: removing whitespace all over --- bash.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bash.html.markdown') diff --git a/bash.html.markdown b/bash.html.markdown index d4f3d424..191f916a 100644 --- a/bash.html.markdown +++ b/bash.html.markdown @@ -252,7 +252,7 @@ grep "^foo.*bar$" file.txt grep -c "^foo.*bar$" file.txt # if you literally want to search for the string, # and not the regex, use fgrep (or grep -F) -fgrep "^foo.*bar$" file.txt +fgrep "^foo.*bar$" file.txt # Read Bash shell builtins documentation with the bash 'help' builtin: -- cgit v1.2.3