Linux and Bash commands
$ echo hello world!File System
On a UNIX system, everything is a file; if something is not a file, it is a process.
More info can be found here
Basic Linux commands
pwd - gives us the absolute path, which means the path that starts from the root.
ls - tells us which files are in the directory you are in.
The -a flag gives us hidden files as well.
cd - changes directory
Notice on line 2 ~/cpp , the ~ stands for the home directory and we are now in the cpp directory
mkdir & rmdir - mkdir makes a new folder and rmdir removes a folder
touch and rm - touch creates a file. rm deletes a file or directory.
Add the -r flag to rm to remove only a directory
Intermediate Commands
echo and cat - echo moves data, usually into files. cat displays the data in files.
Note: You do not need to separate the spaces by using the backward slash here, because we put in >> when we finish what we need to write.
Redirection
Redirect output
> is used to redirect output or output to
Redirect input
< is used to redirect input or to input from
Appending
>> is used to append output to the end of the file.
Multiline
<< file literal or input stream literal
Here we chose EOF as the literal, similiarly we could has wrote stop_input or something else
Pipelines
Pipelines, often called pipes, is a way to chain commands and connect output from one command to the input of the next command. A pipeline is represented by the pipe character: |
The ls command list the files and folders in the current folder.
The wc command list does a word count while the -w flag will list the amount of words.
Here we piped the ls into the the wc command.
Another handy example uses the tee command which stores to file and displays at the same time
A third expample would to use grep which searchs for words and ps which shows processes.
Process Substitution
Instead of comparing two files like this
We can do the following
More on sorting can be found here
Bashing
Simple script
Let us start by creating a script named test.sh and adding some text
Now we have to change give the file execute permission using the chmod command the +x is the same as a+x or all and gives execution to user, group and others read here for more options
Alternativly we can be more specific and use chmod 755 test.sh
the 7 represents the owners permission the second number is the group and the last number is the others permission
1st num
2nd num
3rd num
owner
user
other
The meaning of the numbers:
1
2
4
read
write
execute
If we add up the table above we get the following combinations. So 7 is r,w,x and 5 is r,x
0
1
2
3
4
5
6
7
no permission
execute
write
write and execute
read
read and execute
read and write
read, write, and execute
Lets now look if the permissions have changed by using ls -lh , we'll see the following
Finally lets execute the file using ./ , if the file were in the parent we would use ../ instead
Variables
Lets define a variable in our test.sh script
Important! do not put spaces on either side of
=when defining variable
We can do arithmetic using the $((...)) and access the value use the $ sign
More arithmetic
If we were not to use let the rand would be treated as a string and the output would be 54. The let keyword which allows for variable creation with simple arithmetic evaluation. If you try to assign a string there like let a="hello world" you'll get a syntax error.
We can also do the use the ++var_name inside $((...))
Functions
To define a function use the following template foo(){return}
More complicated function with a return would be
$1 gets the first argument num1. $2 gets the 2nd arg num2 etc. To define a local variable in a function use the keyword local e.g. local num4=$1
IO
use read function for input. Add -p for prompt or what follows will be prompted to user
Multiple inputs look like this
Conditions
((...)) is referred to as arithmetic evaluation. The template is the following if ((...)) ; then. To Finish the if bracket use fi which is if backwards. Note: $[...] is now deprecated
Another example:
Conditions and commands
-e filename - Check for file existence
TODO find commands with ((...)) syntax
Strings
While Loop
Lets do something more complicated. First lets create a file.
Now lets edit our script
done < file.txt pipes the data from file.txt into the while loop
For Loop
Simple C style loop
For Range
Exercise: Lets Print All odd numbers from 1 until 100
Arrays
some_nums[3]=1.618 will append to the array. To append multiple values would can do the following: some_nums+=(1 7)
use @ and # e.g. #some_nums[@] to get number of elements in array
Explanation: If subscript is @ or*, the word expands to all members of name. By prefixing # to variable you will find length of an array (i.e number of elements).
When to use curly brackets?
Curly braces are also unconditionally required when:
expanding array elements, as in
${array[42]}using parameter expansion operations, as in
${filename%.*}(remove extension)expanding positional parameters beyond 9: "$8 $9 ${10} ${11}"
more about curly bracket can be found here
More info about bash can be found here
Regex
Case
Credits to: Basic Linux Commands for Beginners
Last updated