Monday, July 21, 2008

Bash useful tips

Wild card expansion:
  • matches zero or ore characters
  • ? matches any single character
  • [a-z] match a range of characters
  • [^a-z] match all except the range

Command History





The command history is stored in ~/.bash_history file. If user want to re-execute the previous command, they could use simply press up/down arrow key to load them from the history file. There are has other method to help use quickly load the previous command they want.

  • "!number" will execute the command which history sequence number equals number .
  • "^2^1" change first character 2 in last command to 1 in the new command. For example if the last executed command is ping 192.168.1.2 and then execute ^2^1, the hash will execute command ping 191.168.1.2.
  • "~" represent the user's home directory. "cd ~ " go the home directory of the current user. "cd ~username" go to user's home directory however this only can be used by root user.
  • Ctrl + r backward search for match command in the history, user could repeatedly press Ctrl + r unitl find the right one or Ctrl + S search forwardly and Ctrl + G to terminate the search.
  • ESC and than . or ALT + . can recall last argument from previous command


Variable and Curly braces

set - display all the variables
env - display all the environment variables
export - set variable to environment variable
unset - del variable "unset variable_name"
reset - can reset a terminal when the screen get corrupted


Variable
  • $HISTFILESIZE - determines how many commands to be saved in the history file on logout
  • $COLUMNS - sets the width of the terminal
  • $LINES - sets the height of the terminal
  • $HOME - represent home directory of current user.
  • $LANG - set the default language of the shall
  • $PWD - user's current working directory
  • $TREM - set the terminal type
  • $PATH - system default path for searching files. separated with colon(:)
  • $PS1 - prompt setting

    • \d today's date
    • \h short hostname
    • \t current time
    • \u username
    • \w current working directory
    • \! the history number of current command
    • \$ shows if you are a non-privileged user and a # if you are a privileged user
    • \l the basename of the shell's terminal device name which tty.
    • For example if $PS1 = [\u@\h\w]\$ the prompt will be [root@localhost ~]$
{ } usage:
  • touch {q,w} will get files q and w.
  • touch q{1,2} will get files q1 and q2. touch
  • touch {q,w}.{1,2} will get files q.1, q.2, w.1 and w.2
Command Line Expansion:

  • Command Output `` or $(): echo "hostname" display hostname, echo `hostname` or echo $(hostname) display localhost.localdomain.
  • Backslash(\) is escap character and makes the next character literal. Moreover if the backslash is the last character on the line it would means continue command on the next line.
  • Quotes ' or " can inhibit all expansions but " can not inhibit dollar sign $, backslash \, backquotes ` and exclamation point !.
  • Arithmetic $[]: a=2 b=3 echo $[ $a + $b ] bash output 5
  • set -o display bash setting set -o settingname enable the setting. set +o setting disable the setting. For example set -o vi will enable vi style command line editing.
  • gnome terminal shortcut keys: ctrl +shift+t/w open/close new tab; ctrl+PgUp/PgDn change to next/previous tab; ALT + N change to number "N" tab; ctrl+shift+c/v copy/paste command line;
Aliases:

  • Aliases is the easy remembered short name of commands or the combinations of command with parameters. For example alias ll='ls -l'. alias with nor parameter will list all the aliases in the system. "alias aliasname" will show what commands is the aliasname represent.

No comments: