Bashing The Bash!

A longish read …your call.

Well, the title should not indulge you in thinking in terms of “a fanboy’s story”. Because, I am NOT a one, in that sense. I am a technologist and my viewpoints should be neutral and revolve around wholely and solely based on technology offering and understanding.

Alright! As the alarm or heads up in the above paragraph passed, let’s focus on the materials at hand. In this post, I am sticking my neck to provide information about my work environment which revolves around bash and the steps to produce it.

I have picked up bash as my default shell, which is basically no brainier or mere coincident, but just because this is the default one on most of the GNU/Linux systems. So, having it and sticking around with it helps me get along with the system much smoother way. Every other shell has merits and demerits, the same way, this one is no exception.

The good part is that living with it (in fact 90 percent of the time) forces me to bring out the best in it, and also it compels me to see the ugly side too.

The initialization scripts are pretty straightforward forward but when you deep dive and do some scripting, sooner rather than later it quickly gets very complicated and the syntax becomes quite arcane.

But, I had to put some effort into getting along with it. Once you choose something to live on for a long time, you need to put an effort to keep it thriving, like in other serious factors/relations in life. So, on the quest for that, I have developed a love-hate relationship with. Sometimes bash-related stuff makes life so easy and achievable so easily, on the other hand, it handicaps achieving something beyond a certain point. That is probably not Bash’s fault and certainly mine.

Like everyone else, I too fiddle with it initially for not-so-serious or impactful tasks/work on the system. It is driven by two factors, lack of understanding (it is considerably reduced but not gone, and from time to time I am prone to falter or stuck). Anyway, I have come to terms with it and am enjoying whatever little thing I can achieve with it.

I do all my configuration tweaks with it by writing small, independent scripts and then stitching them together to achieve whatever I wanted to achieve. That is the predominant methodology to get along with it. Another reason, I absolutely loathe writing gigantic code bases for something trivial to achieve. To, achieve more out-of-ordinary stuff, I break down problems into smaller pieces and develop each individual piece, and then try to amalgamate all the parts.

I have a public repo uploaded on github, if you haven’t noticed it yet, here is the link to it Scripts repo on GITHUB . I am certainly not claiming everything in that repo is pleasant, but those tidbits work for me. Quite a few of them were borrowed over the years from better people and I have heavily tweaked them to suit my need. Some of them were written by me to fix the damn environment and enhancement from scratch. Some of them I keep it as it is, as those don’t need any improvement for my liking.

I have learned to do it better way(reading to fix my problem) by looking at them and trying to read from their perspective. It is very important to write pieces of code to get your understanding clear, but it is hugely important to read more stuff from others can certainly boost the understanding. Fixing stuff mentioned in the other script can be very educative and alleviate the learning curve. Never mind the trouble, it is the same with every other thing you are putting your hands on.

Thankfully, the internet has its upside, when you stumble upon good resources, which is/were shared by better people and it is free to grab. I have been benefiting like that grandly. And importantly, I am trying to give favor back to the medium for others’ benefit. Sometimes my frivolous work helps others and sometimes they are annoyed by the way I present the stuff. But, that is okay, people are entitled to their opinions.

Now, there are plenty of resources to be found on the internet, not every one of them is good but few are really good. I will provide you some resources at the end of this article for your inspections.

Here is my .bashrc file for bash shell intialization environment . You can take a peek at it.

Here it is entirely for the impatience :

#!/usr/bin/env bash
# Author:  Bhaskar Chowdhury
# Email: unixbhaskar@gmail.com
# Website : https://github.com/unixbhaskar

# Check for existence of global bashrc
if [[ -e /etc/bashrc || -e /etc/bash.bashrc ]]; then

 source /etc/bashrc || source /etc/bash.bashrc

fi
# Bunch of exports
LESSOPEN="|/home/bhaskar/bin/lesspipe.sh %s"; export LESSOPEN
export PATH="$PATH:/home/bhaskar/bin"
#export PILOTRATE=115200
test -s ~/.alias && . ~/.alias || true
export HISTTIMEFORMAT="%h/%d/%Y - %H:%M:%S "
export HISTFILESIZE=99999
export  HISTSIZE=99999
# Aliases
alias ls="ls --color=always"
alias grep="grep --color"
shopt -s checkwinsize cdspell autocd direxpand dirspell dotglob globstar histappend
alias mount="mount | column -t"
alias ports='netstat -tulanp'
alias meminfo='free -m -l -t'
alias psmemhog='ps auxf | sort -nr -k 4'
alias pscpuhog='ps auxf | sort -nr -k 3'
alias fetch='wget -c'
alias rsync='rsync --progress --stats -ravz'
alias c="clear"
alias d='cd ~/Downloads'
alias p='cd ~/Pictures'
alias linuxgit='cd ~/git-linux/linux/'
alias boot='cd /boot'
alias music='cd ~/Music'
alias admscripts='cd ~/Adm_scripts'
alias docu='cd ~/Documents'
alias dstat='dstat -afv'
alias root="sudo su -"
alias sstatus="sudo systemctl status"
alias srestart="sudo systemctl restart"
alias diskinfo="df -h"
alias iptlist='sudo /sbin/iptables -L -n -v --line-numbers'
alias iptlistin='sudo /sbin/iptables -L INPUT -n -v --line-numbers'
alias iptlistout='sudo /sbin/iptables -L OUTPUT -n -v --line-numbers'
alias iptlistfw='sudo /sbin/iptables -L FORWARD -n -v --line-numbers'
alias vpn_start='sudo /home/bhaskar/vpn_connect'
alias dmesg='sudo dmesg -H -T'
alias logs="find /var/log -type f -exec file {} \; | grep 'text' | cut -d' ' -f1 | sed -e's/:$//g' | grep -v '[0-9]

I have cut short the file output here. This file has grown over the years, added, collected, removed, and rewritten many times in the past to make exactly what I want the environment should be.

Now, time for some resources, which I have referred to above somewhere.

Β Heiner’s SHELLdorado

  1. BASH Frequently Asked Questions
  2. GNU Bash manual
  3. How-to: Bash Keyboard Shortcuts
  4. Explain Shell Commands

I hope this will help.

Shell,the interface to your OS kernel

Well,Shell is the lingua franca of accessing the underlying operating system resources. Most people use computers in various forms (mostly those who are technically inclined) to know the underlying mechanism or tap the potentials of the operating system capability offering using the shell to access those facilities.

Most of us, who are living with it for a long, get into it by second nature.No invitation is required πŸ™‚ whenever we are in doubt about doing “not so straightforward” stuff, we just lean onto it and get the damn thing to straighten out.

Its primary job is to translate and convey the words(in the form of commands) you provide on it and pass on the instruction to the underlying layer to fetch the information from OS and return back the results to you on the terminal itself, provided you haven’t directed that output(you can do that with minimum fuss) somewhere else.

Some Terminal lingo

0 is designated as stdin, i.e standard input 1 is designated as stdout, i.e standard output 2 is designated as stderr, i.e standard error

So, having it around is the norm for certain sections of the people involved in computing work. Nowadays, people, who are not so deep into it also have time to time lean on it to find out, what is going on or the opacity of the GUI not allowing them to see through.

Honestly, I haven’t sat or used the proprietary OS for a long long time, due to a lack of inclination and need. That has a shell interface too. Not surprising though. Most, in fact, all of the UNIX systems or variants of it have to have a shell shipped with it. Sometimes, more than one. You can find out easily, showing many shells have been shipped with your OS, like this :

cat /etc/shells
#+RESULTS:
| /bin/bash |
| /bin/tcsh |
| /bin/csh  |
| /bin/ash  |
| /bin/ksh  |
| /bin/zsh  |
| /bin/dash |

See, it has supported these many shells out of the box. I am sitting on Slackware, but it could be true and similar to most Linux distributions, whatever your flavor is.

Now, there is a distinction and disparity between the shells enlisted above but the underlying mechanism or use will be the same i.e to fetch information from the underlying OS and present in human-readable format(not always true, sometimes really arcane and esoteric, there is a reason for that.

It is not the question of why bother learning or understanding it, but it is fact, once you get familiar with it, you can expand the horizon of your search or quest for information, most of the time, very esoteric and technical details of specific things. So, the sooner you can accustomed to it, the better it is for you to delve in the system and manipulate and extract information, not available or accessed by normal means.

For instance, I am going to show you a very very rudimentary script (program written in a different shell’s language or interpreted language) output to make you aware that some of the output of the built-in variable might come in handy to know the environment the shell is using.

#!/bin/bash

printf "This is the name of the file \$\_ $_ \n"

printf "This is the name of the script \$\0 $0 \n"


printf "This is process id of the current script \$\$ $ \n"

printf "This is how you evaluate the status of last command run \$\? $? \n"

if [[ $? == 0 ]];then
        echo it is sucessful
else
        echo it is not
fi

printf "This is how you check number of argument by cross checking against \$\# $# \n"

if [[ $# -ne 2 ]];then

        echo The number of argument is not maching
else
        echo it should operate normally
fi


echo "$USER  -->username"

echo "$LOGNAME  ---> loginname"

echo "$HOSTNAME -----> machine name"

echo "$LINENO  ----> line number"

echo "$RANDOM  ----> Random number"

echo "$SECONDS   ---> Seconds till the script run"
#+RESULTS:
This is the name of the file $_ ./demo
This is the name of the script $0 ./demo
This is process id of the current script $$ 8168
This is how you evaluate the status of last command run $? 0
it is successful
This is how you check number of argument by cross checking against $# 0
The number of argument is not machine
bhaskar  -->username
bhaskar  ---> loginname
Slackware -----> machine name
34  ----> line number
8930  ----> Random number
0   ---> Seconds till the script run

So, you can see from the output(I have made the output curated for you, so it doesn’t distract you with another thing) that, how it is interpolated(that is the term used by the programmers to mean, translate)and provides results. This is probably the simplest script ever written besides “hello world”. πŸ™‚

I have written that piece in bash shell scripting language. As I have mentioned, every other shell has its own language and you can write a script with it. It is absolutely up to you, which shell you chose, which mainly govern by two facts, one, what is your OS provides by default and the second is the familiarity of your understanding of that shell. There is binary that comes along, which allows you to switch between the shell, aptly named, chsh , which is in abbreviated form of change shell. But, most of the naming of commands is not so obvious as this one, in fact, the space is flooded with misnomers. Naming is hard.

I hope you are not having difficulty picking what I am showing here, am trying to make things as simple as possible for you, so you can later use your intelligence to do a more important thing with it. In the above, script, I just print out the default value held by those individual variables.

Likewise, if you are on a shell and do some rudimentary stuff on it get the required information like listing directory, disk space, process information et al. But that interface is not limited to running some predefined binary but to providing you an interface to play with it interactively. This is what most seasoned people(whose bread and butter depend on it aka their job to produce some meaningful results) do for the sake of building something. here are a few interactions with it:

bhaskar@Slackware_10:05:04_Thu Feb 17: :~>iostat
Linux 5.16.1-Slackware (Slackware)      02/17/2022      _x86_64_        (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
          56.83    1.09   27.01    0.26    0.00   14.80

Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
dm-0              0.05         0.76         0.10         0.00      15748       2136          0
dm-1              8.13       136.12       115.16         0.00    2819660    2385504          0
sda              12.07       201.38       277.18         0.00    4171488    5741597          0

…and some more ..

bhaskar@Slackware_10:06:47_Thu Feb 17: :~>cat /proc/version
───────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       β”‚ File: /proc/version
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   β”‚ Linux version 5.16.1-Slackware (root@Slackware) (gcc (GCC) 11.2.0, GNU ld version 2.37-slack15) #1 SMP PREEMPT Tue Jan
       β”‚  18 03:24:37 IST 2022

Or may be ..

bhaskar@Slackware_10:07:56_Thu Feb 17: :~>w
 10:08:35 up  5:48,  2 users,  load average: 4.23, 4.31, 4.40
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1     -                04:21    5:47m  4:11   2:11  /opt/piavpn/bin/pia-wireguard-go --foreground wgpia0
bhaskar  tty2     -                04:21    2:17m 39:13   0.00s xinit /usr/bin/i3 -- /usr/bin/X :0 vt2 -keeptty -auth /home/bhaskar/.serverauth.1538

And probably more esoteric but important information, disk layout

bhaskar@Slackware_10:10:52_Thu Feb 17: :~>lsblk
NAME              MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sda                 8:0    0 931.5G  0 disk
β”œβ”€sda1              8:1    0     1G  0 part /boot/efi
β”œβ”€sda2              8:2    0    20G  0 part
β”œβ”€sda3              8:3    0    20G  0 part
β”œβ”€sda4              8:4    0    25G  0 part /
β”œβ”€sda5              8:5    0   300G  0 part
β”‚ └─vghome-lvhome 252:1    0 299.5G  0 lvm  /home
β”œβ”€sda6              8:6    0   400G  0 part
β”‚ └─vgdata-lvdata 252:0    0 399.7G  0 lvm  /data
β”œβ”€sda7              8:7    0    20G  0 part
β”œβ”€sda8              8:8    0    20G  0 part
└─sda9              8:9    0    30G  0 part

I have been referring to OS quite a bit in the above, which is basically this Operating System. And making it clear that, we are on the same page, so the article produces some sense to everyone alike.

Shell is a kinda bed, where some kind of people sleep, twist, live, and love for their life to do something meaningful. Moreover, the overhead of GUI to bring in action is quite a fold more than something accessing on the CLI . It is comparatively faster to produce the result than in another form.

There is a clear distinction between Shell , Command Line, and System Console ,but one of the things that is very common is their interface looks almost the same with a little varied way of operating on them.

Well, every shell can be configured to the taste of its user. I predominately use bash shell and configured it according to my need. You can find my configuration in the GITHUB dotfiles repo .bashrc .

Here are some references for curious minds πŸ™‚

I hope this will give some sort of heads-up!