Before diving into Bash programming we’ll have a short lecture on Bash Aliases. 1
If you are tired of typing the same long commands over and over again then Bash aliases will be of a 2
great help to you. 3
A bash alias is nothing but a shortcut to a command. 4
Every Linux distribution has at least a few aliases available. To list all aliases 5
just run the alias command without any argument. 6
And we see that there are some aliases for grep, egrep or ls commands. If you run ll, 7
the alias, it will run in fact ls -alF 8
and if you run ls without any argument it will run the alias which is ls --color 9
=auto, it will not run the original command. 10
So an alias with the same name as a command precedes the command. To run the original ls command 11
and not to the alias 12
then write \ and the command name, like this: /ls 13
I'm running the original command, not the alias. 14
The original ls command will display everything in black. 15
This is the original command, and this is the alias. 16
Creating new aliases is very straightforward; the syntax is as follows: alias name = and the command 17
to run, for example, alias c=”clear” I've just created a new alias 18
called C that will run the clear command. It's clearing the screen. 19
Note that if you exit the current terminal or open a new session from another terminal, the aliens 20
will not be available. 21
It's available only in the terminal in which it was defined. To make the aliases persistent, you need 22
to declare them in the .bash_profile or ~/.bashrc files in the user's home directory. / 23
So I'm opening .bashrc from the user's home directory. 24
And, at the end I'm writing, 25
alias now= let's say date +%F\ and a whte space; white space is a special 26
character and it has to be canceled with a backslash and then %T 27
In fact, this command will print out the current date and time. 28
And I'm saving the file. 29
If I'm rebooting the system or opening a new terminal, the aliens will be still available. 30
I'm opening a new terminal and I'm writing now; and it has displayed the current date and time. 31
However, in the terminal in which the alias was defined is not available. 32
That's because .bashrc is read 33
only when the shell starts. We could quit the terminal and open it again or run the file manually 34
so source .bashrc; now the alias is available. To remove an alias in the current session run 35
unalias and the name of the alias like unalias now ; ok! 36
Let's see some other useful aliases. Let's suppose you connect frequently to a remote machine using 37
ssh like this: ssh-p and a random port 38
user1@ and the IP address. 39
I'm writing a random IP address. 40
Instead of memorizing the port and the IP address and write to the entire command each time, you could 41
create an alias, 42
like this: alias and the name of the alias, let's say server1 = and the command between double-quotes. 43
Now, when I want to connect to that remote machine, I'm just running the alias: so server1; of course, 44
it won't work because I've written a random IP address; but it's trying to connect to that remote IP. 45
Another useful one: alias ports = and the command that will display all open parts; so ”netstat 46
-tupan 47
and 48
to quickly become root in the terminal: alias root=”sudo su ” 49
I am root. Or to fully update the system: alias the name of the alias update= and now the commands 50
”sudo apt update && sudo apt dist-upgrade -y && 51
sudo apt clean” 52
Now to update the entire system, I'm running the alias update. 53
I'm stopping it. Or another alias, for ls, which will display the contents of a directory on a single 54
column, sorting the files by their size in human-readable format: alias lt= and the command 55
"ls -hSF for the type --size -1" 56
We'll print out the size of each file and lt or lt/etc 57
lt/ var/ log. 58
That's all about aliases, thank you!