Download BASH Cheat Sheet - Level 2: Commands and Operators and more Cheat Sheet Computer Science in PDF only on Docsity! man command : display the command’s manual page Jacques Dainat -‐ 2015 BASH cheat sheet - Level 2 Miscellaneous \ Escape character. It preserves the literal value of the next character that follows, with the exception of newline. ` command` The backtick (`) is a command substitution. echo The current working directory is: `pwd` >The current working directory is: /home/user/path The text between a pair of backtick is executed by the shell before the main command and is then replaced by the output of that execution. The syntax $(command) is generally preferable. $ It introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces. Using variables variable=value Assign a value value to the variable variable. The variable scope is restricted to the shell. local variable=value Assign a value value to the local variable variable. It doesn’t come out a curly bracket area. export variable=value Make the variable name available to the shell and sub-‐processes. variable=$(command) Assign the output of command to variable. ${#variable} Length of the value contained by the variable. ${variable:N} Keep the character of the value contained by variable after the Nth. ${variable:N:length} Substring the value contained by variable from thr Nth character to up to length specidied. ${variable/pattern/string} The longest match of pattern against the variable value is replaced with string. Print commands echo My home is: $HOME Write arguments to the >My home is: /home/user standard output. echo –e Enable interpretation of backslash-‐ escaped characters. printf Format and print the arguments. printf %q "$IFS" Print the arguments shell-‐quoted. >' \t\n' printf "%.1f" 2.558 Specify the decimal precision. >2.6 printf "%s\t%s\n" "1" "2" "3" "4" %s interprets the >1 2 associated argument 3 4 literally as string. Using quotes Weak quoting -‐ double quote (") : string="My home is: $HOME" echo $string >My home is: /home/user Use when you want to enclose variables or use shell expansion inside a string. Strong quoting -‐ single quote (') : echo 'My HOME is: $HOME' >My HOME is: $HOME Preserves the literal value of each character within the quotes. Wildcards operators Regular expressions : Used to match text. ^ Matches the beginning of the line. $ Matches the end of the line. ^$ Matches blank lines. . Any character. [] Any of the character inside the brackets. [^a-‐f] Matches any character except those in the range a to f. \a A letter (similar to [a-‐zA-‐Z]). \t A tabulation. \n A new line. \w An alphanumeric ([a-‐zA-‐Z0-‐9_]) \W Non alphanumeric (The opposite of \w) ? The preceding item matches 0 or 1 time. * The preceding item matches 0 or more times. + The preceding item matches 1 or more times. {N} The preceding item matches exactly N times. {N,} The preceding item matches N times or more. {N,M} The preceding item matches at least N times and not more than M times. [:class:] POSIX Character Classes ([:alnum:], [:alpha:], [:blank:], [:digit:], etc, respectively equivalent to A-‐Za-‐z0-‐9, A-‐Za-‐z, space or a tab, 0-‐9, etc). Globbing (Pathname expansion) : Used to match filename(s). ? Any single character * Zero or more characters [] Specify a range. Any character of the range or none of them by using ! inside the bracket. {term1,term2} Specify a list of terms separated by commas and each term must be a name or a wildcard. {term1..term2} Called brace expansion, this syntax expands all the terms between term1 and term2 (Letters or Integers). With the extglob shell option enabled (check it with shopt) : In the following description, a pattern-‐list is a list of one or more patterns separated by a |.