summaryrefslogtreecommitdiffhomepage
path: root/bash.html.markdown
diff options
context:
space:
mode:
authorElena Bolshakova <lena-san@yandex-team.ru>2015-06-10 11:34:14 +0300
committerElena Bolshakova <lena-san@yandex-team.ru>2015-06-10 11:34:14 +0300
commit193f66553fc114e83e7c4cfb4607e4a1b57c4f09 (patch)
tree30988e25d31ed6dff83cf409ad093c3c7ec9322c /bash.html.markdown
parent676568cca8731d0dbb2d2bdeff08cc092d283177 (diff)
parent5086480a04d27cff2380f04609210082000538d4 (diff)
Merge branch 'master' of https://github.com/adambard/learnxinyminutes-docs
Diffstat (limited to 'bash.html.markdown')
-rw-r--r--bash.html.markdown78
1 files changed, 45 insertions, 33 deletions
diff --git a/bash.html.markdown b/bash.html.markdown
index 3b163638..08182c2c 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
---
@@ -31,32 +32,41 @@ 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
-# 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 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
-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}
-# This will substitute the first occurance of "Some" with "A"
+echo ${Variable/Some/A}
+# This will substitute the first occurrence 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
-echo ${FOO:-"DefaultValueIfFOOIsMissingOrEmpty"}
-# This works for null (FOO=), 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:
# There are some useful builtin variables, like
@@ -64,16 +74,16 @@ 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?"
-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
@@ -85,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:
@@ -134,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
@@ -142,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
@@ -161,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.";;
@@ -169,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:
@@ -183,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:
@@ -220,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
@@ -235,11 +245,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