Skip to content

Instantly share code, notes, and snippets.

@n1chre
Last active June 29, 2018 17:13
Show Gist options
  • Save n1chre/a65271dd6964d7cd4e68517cdbb3cb18 to your computer and use it in GitHub Desktop.
Save n1chre/a65271dd6964d7cd4e68517cdbb3cb18 to your computer and use it in GitHub Desktop.
Bash stuff: parameter substitution, expansion and manipulation operators; file test operators
Test Description
-e file exists
-f file is a regular file (not a directory or device file)
-s file is not zero size
-d file is a directory
-b file is a block device
-c file is a character device
-p file is a pipe
-L file is a symbolic link
-S file is a socket
-t file (descriptor) is associated with a terminal device (stdin, stdout,...)
-r file has read permission (for the user running the test)
-w file has write permission (for the user running the test)
-x file has execute permission (for the user running the test)
-g set-group-id (sgid) flag set on file or directory
-u set-user-id (suid) flag set on file
-k sticky bit set
-O you are owner of file
-G group-id of file same as yours
-N file modified since it was last read
f1 -nt f2 file f1 is newer than f2
f1 -ot f2 file f1 is older than f2
f1 -ef f2 files f1 and f2 are hard links to the same file
! "not" -- reverses the sense of the tests above (returns true if condition absent)
Command Description
${parameter:-defaultValue} Get default shell variables value
${parameter:=defaultValue} Set default shell variables value
${parameter:?"Error Message"} Display an error message if parameter is not set
${#var} Find the length of the string
${var:offset:length} Substring
${var%pattern} Remove from shortest rear (end) pattern
${var%%pattern} Remove from longest rear (end) pattern
${var#pattern} Remove from shortest front pattern
${var##pattern} Remove from longest front pattern
${var/pattern/string} Find and replace (only replace first occurrence)
${var//pattern/string} Find and replace all occurrences
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment