Using Bash Shell
Command Line Shortcuts File Globbing Globbing is wildcard expansion: * - matches zero or more characters ? - matches any single character [0-9] - matches a range of numbers [abc] - matches any of the character in the list [^abc] - matches all except the characters in the list Predefined character classes can be used
The Tab Key Type Tab to complete command lines: For the command name, it will complete a command name For an argument, it will complete a file name Examples: $ xte $ xterm $ ls myf $ ls myfile.txt
Command Line Shortcuts History bash stores a history of commands you've entered, which can be used to repeat commands Use history command to see list of "remembered" commands $ history 14 cd /tmp 15 ls -l 16 cd 17 cp /etc/passwd. 18 vi passwd... output truncated... More History Tricks Use the up and down keys to scroll through previous commands Type Ctrl-r to search for a command in command history. (reverse-i-search)`': To recall last argument from previous command: Esc,. (the escape key followed by a period) Alt-. (hold down the alt key while pressing the period) !$ (only valid for the last command)
Command Line Expansion The tilde Tilde ( ~ ) May refer to your home directory $ cat ~/.bash_profile May refer to another user's home directory $ ls ~julie/public_html Commands and Braced Sets Command Expansion: $() or `` Prints output of one command as an argument to another $ echo "This system's name is $(hostname)" This system's name is server1.example.com Brace Expansion: { } Shorthand for printing repetitive strings $ echo file{1,3,5} file1 file3 file5 $ rm -f file{1,3,5}
Bash Variables Variables are named values Useful for storing data or command output Set with VARIABLE=VALUE Referenced with $VARIABLE $ HI="Hello, and welcome to $(hostname)." $ echo $HI Hello, and welcome to stationX.
Command Editing Tricks Ctrl-a moves to beginning of line Ctrl-e moves to end of line Ctrl-u deletes to beginning of line Ctrl-k deletes to end of line Ctrl-arrow moves left or right by word
gnome-terminal Applications->Accessories->Terminal Graphical terminal emulator that supports multiple "tabbed" shells Ctrl-Shift-t creates a new tab Ctrl-PgUp/PgDn switches to next/prev tab Ctrl-Shift-c copies selected text Ctrl-Shift-v pastes text to the prompt Shift-PgUp/PgDn scrolls up and down a screen at a time
Scripting Basics Shell scripts are text files that contain a series of commands or statements to be executed. Shell scripts are useful for: Automating commonly used commands Performing system administration and troubleshooting Creating simple applications Manipulation of text or files
Creating Shell Scripts Step 1: Create a text file containing commands First line contains the magic shebang sequence: #! #!/bin/bash Comment your scripts! Comments start with a # Step 2: Make the script executable: $ chmod u+x myscript.sh To execute the new script: Place the script file in a directory in the executable path -OR- Specify the absolute or relative path to the script on the command line
Sample Shell Script #!/bin/bash # This script displays some information about you r environment echo "Greetings. The date and time are $(date)" echo "Your working directory is: $(pwd)"