Chapter 5: Text, editors and the shell environment

Exercise: Customise your shell

On servera, use variables and PATH to run your own command, create an alias, and make your settings survive a new login by adding them to Bash's startup files with vim.

Customise your shell

  • Use and export variables.
  • Run your own script through PATH.
  • Make an alias and environment settings permanent.

Start the exercise

On workstation, start the exercise. It restores student's start-up files on servera to the originals and removes ~/bin.

[student@workstation ~]$ lab start sa-environment

Later tasks assume that you are logged in to the server they name: ssh student@servera from workstation, then sudo -i for a root shell.

See exporting at work

On servera, set a variable, check that a child shell doesn't see it, export it, and check again.

[student@workstation ~]$ ssh student@servera
[student@servera ~]$ TEAM=platform
[student@servera ~]$ bash -c 'echo team: $TEAM'
team:
[student@servera ~]$ export TEAM
[student@servera ~]$ bash -c 'echo team: $TEAM'
team: platform

Run your own command through PATH

~/bin is already in your PATH. Create the directory, write a two-line script into it, make it executable, and run it by name from anywhere. chmod +x marks a file as executable; chapter 7 explains it.

[student@servera ~]$ mkdir ~/bin
[student@servera ~]$ vim ~/bin/up

Type these two lines in insert mode, then save with :wq:

#!/bin/bash
echo "$(hostname -s) has been up $(uptime -p | cut -d" " -f2-)"
[student@servera ~]$ chmod +x ~/bin/up
[student@servera ~]$ cd /tmp
[student@servera tmp]$ up
servera has been up 16 minutes
[student@servera tmp]$ cd

The first line tells the system which program runs the script. Chapter 15 is all about writing scripts.

Add an alias and history time stamps to ~/.bashrc

Open ~/.bashrc with vim, press G to jump to the end, o to open a new line, and add:

alias now='date +%T'
export HISTTIMEFORMAT="%F %T "

Save with :wq, then apply it to your current shell:

[student@servera ~]$ source ~/.bashrc
[student@servera ~]$ now
10:24:15
[student@servera ~]$ history 2
   41  2026-10-03 10:24:12 source ~/.bashrc
   42  2026-10-03 10:24:15 now

Set your editor for every login

EDITOR is an environment variable that programs read, so it belongs in ~/.bash_profile. Add this line at its end with vim:

export EDITOR=vim

Prove the settings survive a new login

Log out and back in, then check all three:

[student@servera ~]$ exit
[student@workstation ~]$ ssh student@servera
[student@servera ~]$ type now
now is aliased to `date +%T'
[student@servera ~]$ echo $EDITOR
vim
[student@servera ~]$ echo "$HISTTIMEFORMAT"
%F %T

A new login shell read ~/.bash_profile, which ran ~/.bashrc, exactly as the startup diagram showed.

Put servera back

Remove the lines you added (in vim, dd on each), or leave them: the next reset restores the original files.

[student@servera ~]$ exit

Grade and finish

Check your work with lab grade sa-environment, then put the exercise away with lab finish sa-environment.