Scripting Classroomnotes 02/Feb/2023

User Shell

Setting up the user shell

  • PS1
    • In the home directory we have file called as .bashrc
    • .bashrc is a configuration file in Linux that contains commands and setings specific to the bash shell. This is executed every time a new bash shell is launched.
    • This can be used to set environmental variables, aliases and other shell customizations
    • backup the existing bashrc file cp ~/.bashrc ~/bashrc.tmp
    • open the bashrc and find PS1
      Preview
    • Lets change this to PS1="QTDevOpsPrompt> "
      Preview
    • Lets change PS1 PS1="\u@\H> \A "
      Preview
    • Refer Here for the PS1 possibilities
    • Shell variable PS1 can be used to customize look and feel of our shell.
    • PS1 is called as primary prompt display variable
  • Alias
    • Lets add the following statements in the bash rc file
      alias proc="ps auwwx"
      alias pfilter="ps auwwx | grep"
      alias start="systemctl start "
      alias stop="systemctl stop "
      alias enable="systemctl enable "
    • proc = list of all processes
    • to filter the process according to keyword
      Preview
    • Now apply and execute
      Preview
  • Adjust the Bash history
    • In the bash rc file as shown in the highlighted section below
      Preview
    • Bash shell remembers 1000 typed commands in history in the current session and to save more than 2000 commands in the history file called .bash_history
  • Adjust the PATH variable
    • We can edit the ~/.profile and PATH for the current user
      Preview
  • SPECIAL VARIABLES in bashrc
    • PS1, PS2, PS3 and PS4
    • HISTSIZE, HISTFILESIZE
    • OLDPWD
    • PWD
  • Refer Here for all the variables and Refer Here for Shell initialization files

Writing our first shell script

  • Lets write a script which prints Hello, Shell Scripting
    Preview
  • add execute permissions sudo chmod +x <filename>.sh
  • cheatsheet Refer Here
  • Refer Here for official docs
  • Refer Here for how to get a shell script to actually work.

Note

  • Lab Environment:
    • ubuntu 20.04
    • centos 8

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner