Thursday, June 23, 2011

Remastering shell

Here you will find basic and more advanced shell (console) usage and some tips to make your work more effective and to save you some time.
 
In this article I assume that you have some basic knowledge about shell (at least you know what it is and you have seen it) .

Executing commands and scripts
First thing you need to know is command execution. Lets assume we want to run program top. All you need to do is type top in the shell and confirm with enter. What about some scripts? Run script like this: ./script_name. This will execute script with it's default interpreter (bash, perl, python) specified in the script header. You can also execute a script with sh script_name (in shell) or python/perl... script_name.

NOTE: don't forget to set executable bit for your script, otherwise you won't be able to run it with "./". To set executable bit use chmod +x script_name for more details see man chmod.

NOTE: get used to Tab key, which will complete or list your commands or paths during typing. e.g. when typing the above mentioned chmod command use: chm<Tab> +x scri<Tab>, you will save yourself a few keyboard clicks...

Working with history
In shell you can list previous commands with the up arrow. and execute them. This is not very useful if you are searching command from let say yesterday. For that you can use command history which will list all previously used commands with numbers. Those numbers can be used to run a command directly by entering !<number>. If you search some specific command in history use grep to filter it. e.g to search for command x11vnc used in the past try: history | grep x11vnc. It will only show you matching entries from history.
Another great feature is history search. Press CTRL+r and enter some part of the command you are searching for. The last matching command will be displayed. If there are several matching commands you can switch between them by pressing CTRL+r. To clear history use history -c.

TIP: if you are writing a long command and suddenly you remember that you need to do something before you can execute it, it would be shame to delete it and then type again. Use CTRL+u to temporarily delete it, make your stuff and then CTRL+y to bring it back.

Managing processes
Each application or script is running as a process and has it's unique PID (process identification). To list processes you can use command ps with various parameters. Use it with ps aux | grep <searched_string>, to grep whatever process you like to find. In the processes list on the left you can see the PID. This number you can use to end processes. When you'd like to kill a process use kill -9 <PID>. To kill process by name use killall my_app_name. On linux there are various process managers, e.g. top or more advanced htop.

Managing jobs (run processes in the background)
Shell is supporting multitasking, so it can run more then one program in a window. You can start your application in the background by adding & after the command (e.g top &). To list your application running in the background use jobs command. To bring the last process in the foreground use fg or to bring in the foreground specific process use fg with number of the job specified: e.g. fg %2. To put actually running application in the background use CTRL+z. The application will be stopped and putted in the background. To run it type bg, you can use bg to start any stopped application in the background with its number: bg %<nr>.

Bash aliases
Alias can be called a shortcut for a command. For example if you often use the same long command, you can specify alias for it so you don't need to type the whole command each time, just execute the alias. The command alias will list all aliases used in your system. To define new alias type: alias lr='ls -lr', now when you run 'lr' command the ls -lr will be executed. This alias will be removed after system restart. You can put alias definition in the ~/.bashrc file and it will be automatically applied during your login to shell.

No comments:

Post a Comment