How do I compare two string variables in an ‘if’ statement in Bash?
For string equality comparison, use: For string does NOT equal comparison, use: For the a contains b, use: (and make sure to add spaces between the symbols): Bad: Good:
For string equality comparison, use: For string does NOT equal comparison, use: For the a contains b, use: (and make sure to add spaces between the symbols): Bad: Good:
As @kirbyfan64sos notes in a comment, /home is NOT your home directory (a.k.a. home folder): The fact that /home is an absolute, literal path that has no user-specific component provides a clue. While /home happens to be the parent directory of all user-specific home directories on Linux-based systems, you shouldn’t even rely on that, given that this differs across platforms: for instance, the equivalent directory on macOS is /Users. What all Unix … Read more
Change into note the space.
There are a number of tools specifically designed for the purpose of manipulating JSON from the command line, and will be a lot easier and more reliable than doing it with Awk, such as jq: You can also do this with tools that are likely already installed on your system, like Python using the json module, and so … Read more
To replace the first occurrence of a pattern with a given string, use ${parameter/pattern/string}: To replace all occurrences, use ${parameter//pattern/string}: (This is documented in the Bash Reference Manual, §3.5.3 “Shell Parameter Expansion”.) Note that this feature is not specified by POSIX — it’s a Bash extension — so not all Unix shells implement it. For the relevant POSIX documentation, see The Open Group Technical … Read more
As far as I know, using & after the command is for running it in the background. Example of & usage: tar -czf file.tar.gz dirname & But how about &&? (example)
Simply type source ~/.bash_profile Alternatively, if you like saving keystrokes you can type . ~/.bash_profile
The N> syntax in Bash means to redirect a file descriptor to somewhere else. 2 is the file descriptor of stderr, and this example redirects it to /dev/null. What this means in simple terms: ignore error output from the command. For example, if kill cannot stop a process because it doesn’t exist, or because the current user doesn’t have the permission to do … Read more
Revised Answer (Feb 12, 2014) Original Answer Caveats: https://stackoverflow.com/a/21210966/89391 From: Using boolean variables in Bash The reason the original answer is included here is because the comments before the revision on Feb 12, 2014 pertain only to the original answer, and many of the comments are wrong when associated with the revised answer. For example, Dennis Williamson’s … Read more
You can enter the long form command: or you can use the shorter version of the command: