Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Linux Shell Scripting Cookbook

You're reading from   Linux Shell Scripting Cookbook Do amazing things with the shell and automate tedious tasks

Arrow left icon
Product type Paperback
Published in May 2017
Publisher
ISBN-13 9781785881985
Length 552 pages
Edition 3rd Edition
Tools
Arrow right icon
Authors (3):
Arrow left icon
Clif Flynt Clif Flynt
Author Profile Icon Clif Flynt
Clif Flynt
Sarath Lakshman Sarath Lakshman
Author Profile Icon Sarath Lakshman
Sarath Lakshman
Shantanu Tushar Shantanu Tushar
Author Profile Icon Shantanu Tushar
Shantanu Tushar
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. Shell Something Out FREE CHAPTER 2. Have a Good Command 3. File In, File Out 4. Texting and Driving 5. Tangled Web? Not At All! 6. Repository Management 7. The Backup Plan 8. The Old-Boy Network 9. Put On the Monitors Cap 10. Administration Calls 11. Tracing the Clues 12. Tuning a Linux System 13. Containers, Virtual Machines, and the Cloud

Customizing bash with configuration files

Most commands you type on the command line can be placed in a special file, to be evaluated when you log in or start a new bash session. It's common to customize your shell by putting function definitions, aliases, and environment variable settings in one of these files.

Common commands to put into a configuration file include the following:

# Define my colors for ls 
LS_COLORS='no=00:di=01;46:ln=00;36:pi=40;33:so=00;35:bd=40;33;01' 
export LS_COLORS 
# My primary prompt 
PS1='Hello $USER'; export PS1 
# Applications I install outside the normal distro paths 
PATH=$PATH:/opt/MySpecialApplication/bin; export PATH 
# Shorthand for commands I use frequently 
function lc () {/bin/ls -C $* ; }

What customization file should I use?

Linux and Unix have several files that might hold customization scripts. These configuration files are divided into three camps—those sourced on login, those evaluated when an interactive shell is invoked, and files evaluated whenever a shell is invoked to process a script file.

How to do it...

These files are evaluated when a user logs into a shell:

/etc/profile, $HOME/.profile, $HOME/.bash_login, $HOME/.bash_profile /
Note that /etc/profile, $HOME/.profile and $HOME/.bash_profile may not be sourced if you log in via a graphical login manager. That's because the graphical window manager doesn't start a shell. When you open a terminal window, a shell is created, but it's not a login shell.

If a .bash_profile or .bash_login file is present, a .profile file will not be read.

These files will be read by an interactive shell such as a X11 terminal session or using ssh to run a single command like: ssh 192.168.1.1 ls /tmp.

/etc/bash.bashrc $HOME/.bashrc

Run a shell script like this:

$> cat myscript.sh  
#!/bin/bash 
echo "Running"

None of these files will be sourced unless you have defined the BASH_ENV environment variable:

$> export BASH_ENV=~/.bashrc 
$> ./myscript.sh

Use ssh to run a single command, as with the following:

ssh 192.168.1.100 ls /tmp

This will start a bash shell which will evaluate /etc/bash.bashrc and $HOME/.bashrc, but not /etc/profile or .profile.

Invoke a ssh login session, like this:

ssh 192.168.1.100

This creates a new login bash shell, which will evaluate the following:

/etc/profile 
/etc/bash.bashrc 
$HOME/.profile or .bashrc_profile
DANGER: Other shells, such as the traditional Bourne shell, ash, dash, and ksh, also read this file. Linear arrays (lists) and associative arrays, are not supported in all shells. Avoid using these in /etc/profile or $HOME/.profile.

Use these files to define non-exported items such as aliases desired by all users. Consider this example:

alias l "ls -l"
/etc/bash.bashrc /etc/bashrc

Use these files to hold personal settings. They are useful for setting paths that must be inherited by other bash instances. They might include lines like these:

CLASSPATH=$CLASSPATH:$HOME/MyJavaProject; export CLASSPATH
$HOME/.bash_login $HOME/.bash_profile $HOME/.profile
If .bash_login or .bash_profile are present, .profile will not be read. A .profile file may be read by other shells.

Use these files to hold your personal values that need to be defined whenever a new shell is created. Define aliases and functions here if you want them available in an X11 terminal session:

$HOME/.bashrc, /etc/bash.bashrc
Exported variables and functions are propagated to subordinate shells, but aliases are not. You must define BASH_ENV to be the .bashrc or .profile, where aliases are defined in order to use them in a shell script.

This file is evaluated when a user logs out of a session:

$HOME/.bash_logout

For example, if the user logs in remotely they should clear the screen when they log out.

$> cat ~/.bash_logout 
# Clear the screen after a remote login/logout. 
clear
You have been reading a chapter from
Linux Shell Scripting Cookbook - Third Edition
Published in: May 2017
Publisher:
ISBN-13: 9781785881985
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image