How to make BASH suit you better

29/12/2020
BASH has a simple standard setup which is great but you may want more! Many computer users avoid the command line, because it is hard to use. This is a misconception. The command line has a learning curve, it requires some knowledge to get started. However, ones you know a few things, it is faster and often easier. After learning a few basic commands, the absolutely essential skill is to read documentation. This may not sound like a skill but it is. The reason is that the documents are generic, they will not answer your specific question, you have to derive the answer from the information you have. When you start your environment, the system has files controlling what your defaults will be in many applications will be set. For bash, you have several files that control this. These file in a sequence and only if others do not exist.

What can you change?

Loads of stuff, but the changes you will notice first are the ones setting your prompt. You also have aliases and environment variables. Many of these are set to make sure you use the correct libraries and executable files when running applications. The files also control and add features to the shell, an example is history. In bash you have a history file that contains the last commands you have entered. You can choose if you want history to keep duplicates and how large the file becomes. There are many more things you can run. A nice example of utilities are available from bash-it.

Where is it stored?

This seems like a simple list of a few files that are run when you start. However, due to the way bash starts, there are a few complications. One is that you want some settings for the system and some for each time you open a shell. The file /etc/profile runs during login, note that it often calls /etc/profile.d/* to set specific values. On Ubuntu, it sets the snap paths, both for where binaries are and where xdg calls applications. This file is system-wide, so don’t use it for personal settings. For system-wide files you also have etc/bash.bashrc, this file is named /etc/bashrc outside of debian based distributions. The administrator sets, hopefully sane, defaults for all users on the system. If you do not agree with those settings, you can override them in ~.bashrc, for the special user.

I know you may be both user and administrator! The next file you need to consider is ~/.profile, this runs at login, not when the shell starts. It also only starts if .bashprofile or .bashlogin does not exist. The standard version checks what shell will be run. When the .profile file runs, it starts ~/.bashrc if it exists. The ~/.bashrc file is where you should set your aliases and other personal settings. Two other files are interesting, ~/.bashlogout and ~/.inputrc, the former runs at logout. It clears the console, by default. More interesting is the inputrc file. Here you change key bindings and key strokes. You can set how you edit on the command line. Default is emacs style editing but you can change that to vi style.

Some examples of changes to make.

Update your prompt… To make your prompt look prettier or to convey more information, you can change the values of PS1. First, you can check what value you have already.

$ echo $PS1

The result looks a little cryptic unless you have set it to a string. Try it:

$ PS1 = "Cool Prompt!"

This is not very useful, you can instead set values that inform you about what is happening in your system. Here is a short table of some values:

u Current username
h Current hostname
w Current working directory
s Name of the shell
t Time in 24-hour format

As a challenge, set your prompt to have your username and hostname, correctly marked with the ampersand. You can also use system defined variables and even the output of scripts. Set a colour scheme… You can have your output in colour, and also have different colours for each type of files. First, create a colourful prompt. The colour can change throughout the prompt. To start a new colour, add ‘e[x,ym’ och stoppa med ‘e[m. Here is an example.

$ PS1="e[0;35m u@h e[m e[0;32m d Ae[m $ >"

umask, how it works… In the shell you have a setting called ‘umask’, it sets how files permissions are set when you create them. The most common value is 022. This makes files have permissions that allow users to read and write and all others to read only. This way, you must change new script files to executable as a separate action. This is a safe way to handle files.

function definitions… You can also incorporate functions, the format of these can be POSIX compliant or bash. If you plan to switch between shells, look up how to stay compliant. You can also run a script in the prompt.

#!/bin/bash
# lsbytesum – the number of bytes in a directory listing
TotalBytes=0
for Bytes in $(ls -l | grep "^-" | awk ‘{ print $5 }’)
do
TotalBytes=$TotalBytes+$Bytes
done
TotalMeg=$(echo -e "n$TotalBytes/1048576 nquit" | bc)
echo -n "$TotalMeg"

If you have the code above (credit to TLDP), you can call it in your prompt(PS1). To set it add this to your bashrc.

$ PS1="[u@h:w ($(lsbytes) Mb)]$ "

You can, of course run it manually to see if you like it first. There are no colours in this style, you must combine many different settings.

Once you have decided what you want, you need to put the values in your .bashrc file.

Conclusion

Bash has many features which you can use to make your environment run better. You can make many jobs faster if you have learned how to be efficient. One way is to create aliases, another is to create your own scripts. It can be very beneficial to your efficiency, if you take the time to climb past the initial barrier.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

String concatenation in bash

The way of joining two or more strings together is called string concatenation. It is a common requirement of any programming...
29/12/2020

Bash `pushd` command

The Linux users may need to switch between many directories for doing a particular task and it is a time consuming task...
29/12/2020

Bash uniq Command

Linux users need to create or read the text file in regular basis for many purposes. A text file can contain different...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024