Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Tutorial Beginning Linux - More on Shell filter_list
Author
Message
Beginning Linux - More on Shell #1
This is the fifth chapter of our Linux tutorial series. We have listed all the tutorials under Linux Beginning Series, here for your convenience. In this chapter we are gonna learn more about the Linux shell, it’s customization, creating aliases and other commands.

What is in this Chapter?
  • Changing Shell Prompt
  • Making a Command Alias
  • Adding Path of a Program
  • Configuring future shells

Sources:
  • Intro Linux
  • Linux Cookbook

1. Changing Shell Prompt

A shell variable is a symbol that stores a text string, and is referenced by a unique name. bash keeps one special variable, named PS1, for the text of the shell prompt. To change the text of the shell prompt, you need to change the contents of the PS1 variable.

Shell variable is a symbol that stores a text string, and is referenced by a unique name. Bash keeps a special variable, called PS1, for the text of the shell prompt. To change the shell prompt, you need to change the contents of the PS1 variable.

This command will output the existing string in the PS1 variable:
Code:
$ echo $PS1

To change a variable’s contents, type its name followed by an equal sign ('=') character and the string that should replace the variable’s existing contents. If your string has spaced in it, use quotes (' ' or " ") to include spaces. To change your shell prompt to 'Your wish is my command: ', type:
Code:
$ PS1='Your wish is my command: '
Your wish is my command:

You can put special characters in the prompt variable in order to output special text. For example, '\w' in the value of PS1 will list the current working directory at that place in the shell prompt text:
Code:
$ PS1='\w $ '
~/Desktop $

The following table lists some special characters and their text output at the shell prompt.
Code:
\a                 Inserts a CTRL+G character, which makes the internal speaker beep. CTRL+G is sometimes called the bell character.)

\d                 The current date.
\h                 The hostname of the system.
\n                 A newline character.
\t                 The current system time, in 24−hour format.
\@                 The current system time, in 12−hour a.m./p.m. format.
\w                 The current working directory.
\u                 Your username.
\!                 The history number of this command.

You can combine any number of these special characters with regular characters when creating a value for PS1. To change the prompt to the current date followed by a space character, the hostname of the system in parenthesis, and a greater−than character, type:
Code:
$ PS1=’\d (\h) >’
14 Dec 1999 (ithaca) >

Changing color of the prompt:

To change the color of the shell prompt you can use:
Code:
$ PS1="\e[0;31m[\u@\h \W]\$ \e[m "

To change it permanently use:

Code:
$ export PS1="\e[0;31m[\u@\h \W]\$ \e[m "

Here is the breakdown of the command:
Code:
\e[ :      Start color scheme.
    x;y :      Color pair to use (x;y)
    $PS1 :    Your shell prompt variable.
    \e[m :    Stop color scheme.

A list of color codes:
Code:
Color         Code
Black         0;30
Blue                 0;34
Green         0;32
Cyan         0;36
Red                 0;31
Purple         0;35
Brown         0;33
Blue                 0;34
Green         0;32
Cyan         0;36
Red                 0;31
Purple         0;35
Brown         0;33

Alternatively, you can edit PS1 variable in /etc/profile file to achieve the same.

You can also use tput command to set terminal and modify the prompt settings. For example, to display RED color prompt using a tput:
Code:
export PS1="\[$(tput setaf 1)\]\u@\h:\w $ \[$(tput sgr0)\]"

Breakdown of command:

tput bold – Bold effect
tput rev – Display inverse colors
tput sgr0 – Reset everything
tput setaf {CODE}- Set foreground color, see color {CODE} table below for more information.
tput setab {CODE}- Set background color, see color {CODE} table below for more information.

Various color codes for the tput command:
Code:
Color {code}        Color
0                        Black
1                        Red
2                        Green
3                        Yellow
4                        Blue
5                        Magenta
6                        Cyan
7                        White

2. Making command alias.

Use alias command to assign an alias, a name that represents another command. Aliases are useful for creating a short command name for lengthy and frequently used commands.

To make an alias of bye for exit command:
Code:
$ alias bye=”exit”

So, now typing ‘bye‘ in the prompt will run the ‘exit’ command instead. You can also include options and arguments in an alias.

To make an alias of ‘ap‘ for the command apropos shell bash shells, type:
Code:
$ alias ap=”apropos shell bash shells”

This command makes ‘ap‘ an alias for ‘apropos shell bash shells‘ in the current shell, so typing ap would run apropos shell bash shells.

3. Adding Path of a Program.

To add or remove a directory in your path, use a text editor to change the shell variable ‘PATH’ in the ‘.bashrc’ file in your home directory.

For example, suppose the line that defines the ‘PATH’ variable in your ‘.bashrc‘ file looks like this:
Code:
PATH=”/usr/bin:/bin:/usr/bin/X11:/usr/games”

You can add the directory ‘/home/geekizer/bin‘ to this path, by editing this line like so:
Code:
PATH=”/usr/bin:/bin:/usr/bin/X11:/usr/games:/home/geekizer/bin”

4. Configuring future shells.

There are a number of configuration files that you can edit to make your configurations permanent. You can also edit these files to specify commands to be run whenever you first log in, log out, or start a new shell. These configuration files are text files that can be edited with any text editor

When you log in, bash first checks to see if the file ‘/etc/profile‘ exists, and if so, it executes the commands in this file. This is a generic, system−wide startup file that is run for all users; only the system administrator can add or delete commands to this file.

Next, bash reads and executes the commands in ‘/etc/bash.bashrc‘ file or ‘.bash_profile‘ (a “hidden” file in your home directory). Thus, to make a command run every time you log in, add the command to this file.

For all new shells after you’ve logged in (that is, all but the “login shell”), bash reads and executes the commands in the ‘.bashrc‘ file in your home directory. Commands in this file run whenever a new shell is started except for the login shell.

Credits: Geekizer
[Image: znALY5V.jpg]

Reply

RE: Beginning Linux - More on Shell #2
With the shell part. You forget about $( command )

i.e
Code:
export PS1="\$( date )";

This works too. The only difference between this and using \h \H \* options is that you have the command in there. I like this method instead of using any of the forward slash aliases. That way I know exactly what is happening in there without looking it up or having it previously in the file. But whatever floats your boat.

Just wanted that information to be in here too.

[+] 1 user Likes Loki123's post
Reply

RE: Beginning Linux - More on Shell #3
'Beginning Linux' is quite misleading since I wouldn't recommend shell to any Linux beginner and definitely not to the difficulty you made shell out to be.

Reply

RE: Beginning Linux - More on Shell #4
(02-04-2016, 01:21 AM)Tirus Wrote: 'Beginning Linux' is quite misleading since I wouldn't recommend shell to any Linux beginner and definitely not to the difficulty you made shell out to be.

Actually, recommending the shell is the first thing I recommend and there are several reasons why one should have a fair knowledge on the shell.

Analyzing issues, modifying startup applications, installing applications and searching for files and directories, along with a lot more that you can do in the shell both faster than using the GUI, and in many distributions not able to do with GUI tools. Most problems new users will encounter will require them to use the shell to fix it. GNU/Linux relies heavily on the shell, so it is very important actually to have at least a basic understanding on how it works and what it can do. For that reason, it is the first thing I recommend, to use the shell as much as you can. It is too involved with the system in general (including Ubuntu, Mint and other newbie distributions) to not be one of or the first thing you try to use and learn to use.

[+] 1 user Likes Loki123's post
Reply







Users browsing this thread: 2 Guest(s)