How do I set a variable to the output of a command in Bash?

In addition to backticks `command`, command substitution can be done with $(command) or “$(command)”, which I find easier to read, and allows for nesting. Quoting (“) does matter to preserve multi-line variable values; it is optional on the right-hand side of an assignment, as word splitting is not performed, so OUTPUT=$(ls -1) would work fine.

echo that outputs to stderr

You could do this, which facilitates reading: >&2 copies file descriptor #2 to file descriptor #1. Therefore, after this redirection is performed, both file descriptors will refer to the same file: the one file descriptor #2 was originally referring to. For more information see the Bash Hackers Illustrated Redirection Tutorial.

How to trim whitespace from a Bash variable?

I have a shell script with this code: But the conditional code always executes, because hg st always prints at least one newline character. Is there a simple way to strip whitespace from $var (like trim() in PHP)? or Is there a standard way of dealing with this issue? I could use sed or AWK, but I’d like to think there is a more elegant … Read more

How can I match a string with a regex in Bash?

To match regexes you need to use the =~ operator. Try this: Alternatively, you can use wildcards (instead of regexes) with the == operator: If portability is not a concern, I recommend using [[ instead of [ or test as it is safer and more powerful. See What is the difference between test, [ and [[ ? for details.

mvn command not found in OSX Mavrerick

Try following these if these might help: Since your installation works on the terminal you installed, all the exports you did, work on the current bash and its child process. but is not spawned to new terminals. env variables are lost if the session is closed; using .bash_profile, you can make it available in all sessions, since when a bash session starts, it … Read more

Running Bash commands in Python

On my local machine, I run a python script which contains this line This works fine. Then I run the same code on a server and I get the following error message So what I did then is I inserted a print bashCommand which prints me than the command in the terminal before it runs it with os.system(). … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)