Thursday, October 14

Linux user environment (The Shell)

1. Log in to CLI

  • When a user logs in to the system and accesses the CLI, an environment provided by the system to the user to perform different tasks, it is called ‘Shell‘. If you log in using the CLI utility, the system prints the prompt ‘#‘ or ‘$‘ along with the username and hostname.
  • The difference between the two prompts is, ‘#is for superuser i.e. root. ‘$‘ is for the rest of the users.
  • For ordinary users
  • For user root
  • You type commands that get executed, it is not the doing of the kernel, but, a command interpreter i.e. shell.
  • Shell is just a program of Linux, with some additional features.
  • There are few benefits of having a shell between you and the kernel. Like filename shorthand, input-output direction, and personalizing the environment.

2. Filename shorthand

  • There could be files with the same pattern of names in your system, you can enter the set of filenames as an argument to any command. The shell will identify or find the filenames that match your pattern.
  • e.g. I have 10 files with names file1, file2, file3…… file10. You can have files with other names, sometimes names can be lengthy. You can make mistakes while typing the whole name manually. This is where filename shorthand comes into play.
  • The shell takes the ‘*‘ to mean ‘any string of characters‘ so ‘file*‘ is a pattern that matches all file name in the current directory that begins with ‘file’.
  • The shell creates the list in alphabetical order by default.
  • We can use the same feature with other commands as well, which I will be covering later.

3. Input-Output redirection

  • Most of the commands in Linux produce output on the terminal.
  • With this benefit of the shell, we can redirect the output of a command into a file.
  • The symbol ‘>‘ means to put the output of the command in the following file. Here we are placing the output of the command ‘ll’ into a file i.e. output. The file will be created if it doesn’t already exist.
  • If I read the output file, I can see the same result from the 2 screenshots above.
  • You can combine several files into one by capturing the output of a command in a file.
  • >>‘ symbol means to append the result at the end of the file.

4. Pipes

  • Using pipes you can put the output of one program into the input of another program without any temporary file.
  • The programs in a pipeline actually run at the same time, not one after another.
  • The kernel looks after whatever scheduling and synchronization are needed to make it all work.
  • Most of the commands fit into this model. The only exceptions are commands like date and who that read no input and a few like diff that have a fixed number of the inputs.

5. Processes

  • An instance of a running program is called a process. Shell assigns a number to each process, it is called process-id.
  • It’s important to get clear between program and process. ls is a program, each time you run the command, it creates a new process.
  • If several instances of the same program are running, each will contain a different process-id.
  • You can use the process-id to stop a process with the ‘kill’ command. If you don’t know the process-id, you can use the ‘ps’ command.
  • Command ‘kill 0’ will kill all processes except your login shell.

6. Tailoring the shell environment

  • All users are having their home directory and some default files which are responsible for environment behavior.
  • When you log in to the system, it takes you directly to your home directory.
  • For root, it is /root and for other users, it is /home/username.
  • You will have a .bash_profile file in your home directory. You can put commands into it to set up your environment as you like and they will be executed every time you log in.
  • The most useful shell variable is the one that controls where the shell looks for the commands. When you type a command, the shell looks for it first in the current directory, then in /bin, then on /usr/bin.
  • This sequence is called the search path and is stored in a shell variable called PATH.
  • Default .bash_profile looks like below.
  • To change or modify the PATH variable, enter a new line in your .bash_profile.
  • $PATH will hold the previous values and /home/linuxhawks/blog1 will be added to it as a new full path. Finally, you need to export the value into the shell for changes to take effect.

The best way to get used to all of this stuff is to practice on your system. The more time you spend, the more you will learn.

Leave a Reply

Your email address will not be published. Required fields are marked *


Enjoy this blog? Please spread the word :)