Bash

From ConShell
Jump to navigation Jump to search

The infamous Bash shell (bourne again shell) is the typical default command shell on Linux systems.

You can read up on how bash works using man bash

A great online reference for the set options (i.e. Built-in) can be found at
https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html

Tips

Here is a tip. Bash will read different rc files on starting based on whether or not it is a "login" shell.

The difference is evident in the following example:

ssh host
(a login shell is created i.e. /bin/bash -l)
-vs-
ssh host "echo true"
(instead of executing a login shell the command echo true runs instead)

The first example (login shell) will read from /etc/profile then ~/.bash_profile for startup commands.

The second example will read from /etc/bash.bashrc then ~/.bashrc for startup commands.

So, long story short, I tend to put the salient commands in ~/.bashrc and source it from ~/.bash_profile

~$ cat ~/.bash_profile
#Since .bashrc is not read when login over ssh we need this
if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi

~$ cat ~/.bashrc
#Yes indeed, most bash customizations belong here
 export EDITOR=vim

ssh-agent

To get ssh-agent started cleanly on login, use a stanza like this in your .bashrc

SSH_ENV="$HOME/.ssh/environment"

function start_agent {
     echo "Initialising new SSH agent..."
     #/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
     #as above, but less quiet for debugging --mdf
     /usr/bin/ssh-agent > "${SSH_ENV}"
     echo succeeded
     chmod 600 "${SSH_ENV}"
     . "${SSH_ENV}" > /dev/null
     /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
     . "${SSH_ENV}" > /dev/null
     #ps ${SSH_AGENT_PID} doesn't work under cywgin
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
         start_agent;
     }
else
     start_agent;
fi

sh vs. bash vs. dash

You might think sh->bash but it (sometimes) links to dash. Be careful invoking /bin/sh in shell scripts that need bash functionality.