1
0
Fork 0
mirror of https://github.com/tldr-pages/tldr.git synced 2025-03-28 21:16:20 +01:00
tldr/pages/common/if.md
2021-03-08 20:06:24 +00:00

776 B

if

Simple shell conditional. See also: test, [.

  • Execute two different commands based on a condition:

if {{command}}; then {{echo "true"}}; else {{echo "false"}}; fi

  • Check if a variable is defined:

if [[ -n "{{$VARIABLE}}" ]]; then {{echo "defined"}}; else {{echo "not defined"}}; fi

  • Check if a file exists:

if [[ -f "{{path/to/file}}" ]]; then {{echo "true"}}; else {{echo "false"}}; fi

  • Check if a directory exists:

if [[ -d "{{path/to/directory}}" ]]; then {{echo "true"}}; else {{echo "false"}}; fi

  • Check if a file or directory exists:

if [[ -e "{{path/to/file_or_directory}}" ]]; then {{echo "true"}}; else {{echo "false"}}; fi

  • List all possible conditions (test is an alias to [; both are commonly used with if):

man [