Shell
Check and change your shell
$ echo $SHELL
$ echo $0
Change your default shell (e.g. to zsh):
$ chsh -s /usr/bin/zsh
POSIX Shell
The POSIX (the Portable Operating System Interface) shell is the standard Unix shell, i.e. it was formally defined and shipped in a published standard, it has many competing implementations on many different operating systems, but are compatible.
POSIX shell is basically Bourne shell, lives at the standardized location /bin/sh
.
The POSIX standard does not recognize long flags like grep --file=FILE
, but only the short flags like grep -f
. (Because it does not define getopt_long
function, only getopt
function).
Bash: Bourne Again Shell
In many Linux distros, bash is the default shell, and /bin/sh
is symlinked to /bin/bash
.
Bash is not standardized, but there's only one implementation of bash (i.e. bash is defined by its implementation).
Shebang
The #!
is called a shebang. Scripts will execute using the interpreter specified on a first line.
Make Shell Script Portable
If you use the POSIX shell /bin/sh
, just add #!/bin/sh
to the top of your script. The location is standardized.
If you use other shells, e.g. bash, use #!/usr/bin/env bash
instead of #!/bin/bash
as shebang. Because bash is not always in the same location. If you have multiple versions of a shell installed, use env
will make sure the first executable in the PATH
will be used.
Set Environment Variable
$ x="hello" y="world" bash -c 'echo $x $y'
hello world
bash -c
: "commands are read from the first non-option argument command_string. If there are arguments after the command_string, the first argument is assigned to 0 sets the name of the shell, which is used in warning and error messages."
sshuttle
Allows you to create a VPN connection from your machine to any remote server that you can connect to via ssh.
You must have root access on the local machine, but you can have a normal account on the server.
Shortcuts
grep | wc -> grep -c
sort | uniq | wc -> sort -u | wc
env
List all variables
$ env
TERM=xterm-256color
SHELL=/bin/sh
JAVA_HOME=...
HADOOP_HOME=...
env
is equivalent to printenv
$ printenv
Show A Specific Variable
$ env | grep TERM
xterm-256color
or
$ echo $TERM
xterm-256color
Remove A Variable
$ unset <VAR_NAME>
List of Built-in Variables
HOME
- Home directory of the user.MAIL
- Contains the path to the location where mail addressed to the user is stored.IFS
- Internal Field Separator. Contains a string of characters which are used as word separators in the command line. The string normally consists of the space, tab and the newline characters. To see them you will have to do an octal dump:$ echo $IFS | od -bc
PS1 and PS2
- Primary and secondary prompts in bash. PS1 is set to$
by default and PS2 is set to>
. To see the secondary prompt:$ ls |
USER
- User login name.TERM
- indicates the terminal type being used. This should be set correctly for editors like Vim to work correctly.SHELL
- Determines the type of shell that the user sees on logging in.LOGNAME
- login name
Search
Ctrl-R
: search backward.Ctrl-S
: search forward.
For example, to search grep
commands in your history and cycle through alternatives:
Ctrl-R
grep
Ctrl-R
Ctrl-S