Unit 6 - Using the bash Shell

From Msamir projects

Jump to: navigation, search

Contents

What is shell ?

Sure i am not ask about that gas station in you street, if you think i am pleas close this page right NOW!!!

Every Operating System have different designs in linux kernel is the core of the operating system. It contains all the basic, low-level things that are needed to make the computer work, and provides a framework for all other software to run on. some of this software provide services like web server , mail server other for fun like games other for graphics and so on. Some of this software provide user interface this user interface take commands form user and work with kernel to finish what user intend to do this type of user interface called shell and as it obvious now where the name come from.

A Linux shell is both a command interpreter and a programming language. Shells may be used interactively or non-interactively. In interactive mode, they accept input typed from the keyboard. When executing non-interactively, shells execute commands read from a file.

To show all available shells in your system use this command

[msamir@server1 ~]$ cat /etc/shells 
/bin/sh
/bin/bash
/sbin/nologin
admin@debian:~$ cat /etc/shells
# /etc/shells: valid login shells
/bin/csh
/bin/sh
/usr/bin/es
/usr/bin/ksh
/bin/ksh
/usr/bin/rc
/usr/bin/tcsh
/bin/tcsh
/usr/bin/esh
/bin/bash
/bin/rbash
/usr/bin/screen

in general shells run system utility and commands also it provides some built-in futures and commands too, each shell does the same job, but each understand a different command syntax and provides different built-in commands.

what is my current shell ?

[msamir@server1 ~]$ echo $SHELL 
/bin/bash
<pre>
<pre>
admin@debian:~$ echo $SHELL
/bin/bash

as you see BASH is the default shell in almost all Linux distribution now a days include RedHat - Fedora ... this is why we will study bash

Bash

Bash is the shell, and as all other shell so it can work as command interpreter or a programming language. Bash name come from "Bourne-Again SHell". Bash is intended to be a conformant implementation of the ieee posix Shell. It offers functional improvements over sh for both interactive and programming use.

Bash Variables

many of bash behavior and functions can be control throw global variables, you can easily display the contents of any shell variable, by using the echo command with the variable name preceded by the $ symbol. Some of this variables.

  • PS1 the primary prompt string
[msamir@server1 ~]$ echo $PS1
[\u@\h \W]\$

Let us try to set the prompt so that it can display today’d date and my name:

[msamir@server1 ~]$PS1="\d MSamir$"
Thu Jun 25 MSamir$ls
Mail
Thu Jun 25 MSamir$
  • PATH A colon-separated list of directories in which the shell looks for commands. A zero-length (null) directory name in the value of PATH indicates the current directory. A null directory name may appear as two adjacent colons, or as an initial or trailing colon.
[msamir@server1 ~]$ echo $PATH
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/msamir/bin
  • OLDPWD The previous working directory as set by the cd command.

User interface.

PWD The current working directory as set by the cd command.

redhat:~# pwd
/root
redhat:~# echo $OLDPWD
/root
redhat:~# echo $PWD
/root
redhat:~# cd /tmp/
redhat:/tmp# echo $OLDPWD
/root
redhat:/tmp# echo $PWD
/tmp
redhat:/tmp# pwd
/tmp
redhat:/tmp# cd
redhat:~# echo $OLDPWD
/tmp
redhat:~# pwd
/root
redhat:~# echo $PWD
/root
  • HOME The home directory of the current user; the default argument for the cd builtin command. The value of this variable is
             also used when performing tilde expansion.
rhce:~# echo $HOME
/root
rhce:~# whoami 
root

Bash line completion

Thanks to bash completion you don't have to type the full command name or you not have to keep typing full path to some files the same for arguments, using the Tab Key make life really much more easer for all Linux users and administrator, Tab key provide auto complete future in bash for example when i need to use command like "bluetooth-properties" i don't have to type all this command i only type "blue" then press tab key it will auto complete my word to be bluetooth-properties.

What if there is many command in my system begin with blue ?? bash in this case will ask me which one you mean

RHCE-RedHat:~# blue<TAB>
RHCE-RedHat:~# bluetooth-<TAB>
bluetooth-analyzer    bluetooth-applet      bluetooth-properties  bluetooth-sendto      
RHCE-RedHat:~# bluetooth-p<TAB>
RHCE-RedHat:~# bluetooth-properties 

Auto complete not only working in command in working also in file name and paths for example:

rhcelab:~# cat /pr<TAB>
rhcelab:~# cat /proc/<TAB>
Display all 158 possibilities? (y or n)
rhcelab:~# cat /proc/n<TAB>
rhcelab:~# cat /proc/net/<TAB>
anycast6       if_inet6       ip6_mr_vif     mcfilter       packet         raw6           snmp           stat/          udp6
arp            igmp           ip_mr_cache    mcfilter6      protocols      route          snmp6          tcp            udplite
dev            igmp6          ip_mr_vif      netfilter/     psched         rt6_stats      sockstat       tcp6           udplite6
dev_mcast      ip6_flowlabel  ipv6_route     netlink        ptype          rt_acct        sockstat6      tr_rif         unix
dev_snmp6/     ip6_mr_cache   irda/          netstat        raw            rt_cache       softnet_stat   udp            wireless
rhcelab:~# cat /proc/net/a<TAB>
anycast6  arp       
rhcelab:~# cat /proc/net/ar<TAB>
rhcelab:~# cat /proc/net/arp 
IP address       HW type     Flags       HW address            Mask     Device
192.168.1.1      0x1         0x2         00:1o:s5:9d:a3:f6     *        eth1
rhcelab:~# 

Bash line editing

Bash line history

Tilde

history

Tilde ( ~ )

Curly braces: { }

echo

« RedHat Linux Essentials
Using the bash Shell
»
Users, Groups and Permissions Standard I/O and Pipes
Personal tools