Bash - (Simple) (Command | Expression)
> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)
Table of Contents
1 - About
Language - Expression in bash are Command
A command (or an expression) is a sequence of words separated by blanks, terminated by a control operator.
For the builtin bash function command, see Bash - Command (The Builtin command command)
Localization: Bash will search in this order
If you want to disable a builtin command, see the command Bash - Enable, disable or print Builtin command
Each command execution has an environment.
2 - Articles Related
3 - Order
The default order for command lookup is:
- with scripts and executables last.
There are three built-ins that you can use to override this order:
- Bash - Command (The Builtin command command) - Removes alias and function lookup. Only built-ins and commands found in the search path are executed
- Bash - Builtin Commands - Looks up only built-in commands, ignoring functions and commands found in PATH
- and Bash - Enable, disable or print Builtin command - Enables and disables shell built-ins
4 - Exit Status
The return status (of Exit Status) is:
- its exit status as provided by the POSIX 1003.1 waitpid function,
- or 128+n if the command was terminated by signal n.
5 - Management
5.1 - Execution
5.2 - Check availibilty
# Check the command $1 function check_command() { command -v $1 -v 2>&1 > /dev/null || { echo >&2 "I require $1 but it's not installed. Aborting."; exit 1; } }
5.3 - Help
5.4 - Alias
To create a shortcut of a command with parameters, see Bash - Alias (of a command) - Builtin command
5.5 - List the history of the command called
5.5.1 - history
With the command history:
$history
you will get an history of the command
1000 cat installation_overview_di.txt 1001 fc -l 999 1002 man fc 1003 history 1004 history|grep NQS 1005 ls -al 1006 ls -al 1007 dir 1008 history
The exclamation mark start a history command substitution, except when followed by a space character, tab, newline, = or (.
- !! execute the last command executed. It would execute the history command (1008)
- !str - Re runs the last command that you ran that starts with str (string). For instance, !ls would execute the last ls command ie the command 1006
- !1007 Rerun the command 1007. A dir command in our case.
- !-2 Refer to the current command line minus 2. Ie the command 1007 (dir)
- !?str? - Refer to the most recent command containing str (string). For instance, !?al? will start the command 1000 and not the command 1006 because it does not take into account the option !
5.5.2 - fc
The 'fc' utility lists or edits and re-executes, commands previously entered to an interactive sh.
fc -l 999
will list the last 999 command
5.5.3 - Ctrl + r
6 - List of common command
Command | Description |
---|---|
Network | |
ifconfig | To detect the IP Adress |
netstat -rn | routing table |
Ctrl+C | interruption of script |
hostname name | To set the hostname, name is the system name you want for your local machine (fully qualified domain name or not) |
Navigation / NFS | |
mkdir | make directory |
cd | change directory ( cd .. is the shortcut for moving up one directory level). ex. cd “Entreprise Linux 2007” |
ls | ls -a show the cached file |
mv | Changes the names of directories and subdirectories mv old_name new_name |
pwd | display the name of the current directory. The pwd command stands for “print working directory” |
which | give the path of a file. Ex : “which java” return “/usr/bin/java” |
ln | make links (short cut) between files |
df | Disk Free to know the used space (Example: df -h where h stands for human readable) |
rm | delete a file |
rmdir | delete a directory |
CD Rom | |
mount | On redhat linux 6 or below: mount /dev/hdc /mnt/cdrom and for above versions: mount /dev/cdrom /mnt/cdrom |
Environment | |
echo | To get the value of en environment variable (ex.: echo $ARBORPATH) |
env or set | A long list of bash environment variables appears |
Security | |
whoami | Return the current user |
Process | |
top | display top CPU processes |
Text Manipulation | |
grep | filters content based on certain strings |
sed | search and replace |
awk | sed and grep in one tool |