Category Archives: network
Udemy - git
Udemy - Network-Linux
Udemy - Cybersecurity
Linux - Resources *****
Bash prompt basics
- Super User
- Programming & Scripting
Contents
Introduction
When managing a linux/unix operating system from the command line, users are interacting with the system via shell. This article will explore some of the basic features of the bash shell prompt. Default bash command line prompt on many linux systems does not have a color and display information about user's username, hostname and current working directory. As you well see in the following sections of this document this default settings can be easily changed by exporting a bash prompt PS{n} variables. Bash prompt can be modified to suit users needs and can display time, load, number of users using the system, uptime and more.
Bash prompt variables
As anything else in the linux system also bash prompt can be customized. Bash prompt can be customized by changing the values of bash PS1, PS2, PS3, PS4 variables. To keep the things simple, this article will be concerned just with PS1 and PS2 variables. Use echo command to see the current value of PS1 and PS2 variables.
echo "Bash PS1 variable:" $PS1 echo "Bash PS2 variable:" $PS2
Bash PS1 prompt variable
PS1 is a primary prompt variable. Currently it holds \u@\h:\w\$ special bash characters. This is the default structure of the bash prompt on many Linux systems and is displayed every time you log in using a terminal.Please see the following section "Bash prompt special characters" for explanation of \u, \h, \w and \$ symbols. Here is a classical bash prompt with default settings:
Bash PS2 prompt variable
PS2 bash shell variable is a secondary prompt. This prompt is displayed for if the shell waits for a user input, for example your forget to insert second quotation.
Bash prompt special characters
Bash prompt can be customized by use of special characters. This table contains bash prompt special characters and its explanation.
Bash special character | Bash special character explanation | Bash special character | Bash special character explanation |
---|---|---|---|
\a | an ASCII bell character (07) | \d | the date in "Weekday Month Date" format (e.g., "Tue May 26") |
! | begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt | ||
\D{format} | the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required |
Bash prompt customization
After user login to the systems user environment variables initialized from various files like:
- global system files /etc/profile or /etc/bashrc
- user files ~/.bash_profile , ~/.bash_login , ~/.profile , ~/.bashrc or ~/.bash_logout
It is important to know that all users environment variable have a life time equal to the terminal session. When the terminal session is closed the user's variables including bash shell variables defined during a terminal session are emptied and a again redefined when new terminal session is created either via logo in shell or interactive shell. Lets define two variables to prove this statement.
Permanent bash variable definition
First permanent variable we define in one of the bash initialization files ~/.bash_profile and second variable we define on the shell prompt. Let's define permanent user variable:
What happened here, is that user "prompt" modified its own .bash_profile initialization file located in his/her home directory by appending a VAR1 variable definition. When user "prompt" logged out and logged in again the $var1 variable is initialized and available for the new terminal session.
On the same principles we can define our bash prompt. The best place to do it is that bash initialization file .~/bashrc. open up your ~/.bashrc file and add/edit the line defining a PS1 variable to something like:
PS1='MY NEW BASH PROMPT@\t:\w$ '
NOTE: Your ~/.barshrc file may differ from the example below !
After the user "prompt" restarts a terminal session he/she is welcomed by new bash prompt. This changes to the bash prompt are permanent.
HINT: Use "source" command to re-read a bash initialization .bashrc file instead of restarting a terminal session.
source .bashrc
or similarly
. .bashrc
Temporary bash variable definition
Temporary bash variable definition is defined only for a lifetime of the current terminal session. This is tome by an export command.
As you can see the variable $VAR2 is not defined when user closes his/her terminal session. The permanent variable $VAR1 is always defined from the bash initialization file: ~/.bash_profile . As we can use an export command to define new bash variables we can also use it to modify a bash prompt $PS1 variable. To change a current bash prompt to display only time we could do:
export PS1="\t: "
Changing foreground and background bash prompt colors
Syntax for changing colors in the bash is as follows:
3[ -> Indicates the beginning of color in the text x;yzm - Indicates color code 3[00m - Indicates the end of color in the text
Bash color codes:
export PS1="3[01;31mBASH IN RED3[00m: "
Bash Prompt Examples
To get you started with your new bash prompt here are couple examples:
Display current Time
export PS1="\u@\h \t:$ "
Counting Files in the Current Directory
This bash prompt displays current number of files and directories in the current directory.
export PS1="\u@\h [$(ls | wc -l)]:$ "
The Ultimate Guide to Home Networking
Networking Essential
Ubuntu quick notes
From: Computer Beginner's Guides
Terminal: CTRL+ALT+T keys simultaneously
Once the Termina launches, type this command and then hit the ENTER key:
ifconfig
The ifconfig command will generate an output with a great deal of information.
Fortunately, most of it is useful. The “eth0″ refers to the first Ethernet connection on your system. The “indet addr” displays your system’s IP address, while “Mask” shows the subnet mask. “HWaddr” shows your Ethernet adapter’s MAC (Media Access Control) address, which is (theoretically) unique to each adapter. (Some wireless networks require you to supply your MAC address before allowing your system to connect.)
To find the MAC address:
ifconfig | grep HWaddr