Different between " ; " and " && " in bash [duplicate] - bash

This question already has answers here:
What is the difference between double-ampersand (&&) and semicolon (;) in Linux Bash?
(4 answers)
Closed 2 years ago.
I've been doing lots of Linux based stuff with my time and I know that the ; is used to separate commands, and && runs command after the previous one is done.
But if anyone more knowledgeable then me can explain the difference between the two, that would be nice.
Here's a simple example:
whoami ; hostname
whoami && hostname

; will execute the second command whether or not the first returns without error.
&& is the bash AND logical operator, and will execute the second command only if the first returns succesfully without error.
The success of a command is determined by its exit status.

Related

Bash executes comands out of sequance sometimes [duplicate]

This question already has answers here:
Why didn't the shell command execute in order as I expect?
(4 answers)
bash script order of execution
(2 answers)
Closed 3 years ago.
I running multiple commands in bash in parallel and need to get output that is delimited so receiving script could separate the values.
I attempted to do this in few ways but it seems that any echo is executed instantly and anything following after.
So I am trying to find a way to separate input from each output with separator preceding output.
I actually use curl request that may take 50-200ms to respond, but here for simplicity I will give example with time command.
Here is rough example:
echo ">" && time &
echo ">" && time &
echo ">" && time &
wait
This produces >>> time time time
I am looking for a way to make it produce >time>time>time
I had some success trying to call other bash scripts with trailing echo command instead of making actual commands and that works most of the time but inevitably things get mixed up because of timing.
I will post updates as I work on it, thank you for the help
Try this:
echo ">$(time)" &
echo ">$(time) &
echo ">$(time)" &
wait
That tells echo that it needs the output of the time command you have before it can do its thing.

rsync remote to local - Unexpected remote arg [duplicate]

This question already has answers here:
How to execute a bash command stored as a string with quotes and asterisk [duplicate]
(5 answers)
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 4 years ago.
I'm trying to assemble an rsync command in a bash variable and then execute it.
It looks something like this:
CMD="$RSYNC -a $REMOTE $LOCAL $LINK_DEST"
It gets executed like this
RSYNC_RESULT=$($CMD)
This works fine until I try to add add --rsync-path="sudo /usr/local/bin/rsync" to the mix (so that rsync runs as root on the remote).
RSYNC_PATH='--rsync-path="sudo /usr/local/bin/rsync"'
CMD="$RSYNC -a $RSYNC_PATH $REMOTE $LOCAL $LINK_DEST"
Now I get an error
Unexpected remote arg: user#remote.local:/Users/user/files/
rsync error: syntax or usage error (code 1) at main.c(1343) [sender=3.1.2]
I'm fairly certain it's connected to the quoting in the $RSYNC_PATH var and/or the $($CMD) bit, because I can paste the resulting command into a shell and it runs successfully.
Any ideas what I can do to make this work?

`set` options in shell [duplicate]

This question already has answers here:
Automatic exit from Bash shell script on error [duplicate]
(8 answers)
Specific man page for shell built-in commands like 'source' [duplicate]
(1 answer)
Closed 4 years ago.
I often see set -e or set -ex in dockerfiles and I was wondering what their purpose was. Recently, I've also seen set -o pipefail, and I have no idea what it does. I tried man set to see if there was any manual description, but there wasn't any and I am resorting to ask here.
You can find the full list here:
https://ss64.com/bash/set.html
-e
Exit immediately if a simple command exits with a non-zero status, unless the command that fails is part of an until or while loop, part of an if statement, part of a && or || list, or if the command's return status is being inverted using !. -o errexit
-x
Print a trace of simple commands and their arguments after they are expanded and before they are executed. -o xtrace
-o pipefail
If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.

Bash: Set up a new command on a new line without executing [duplicate]

This question already has an answer here:
How to prefill command line input
(1 answer)
Closed 6 years ago.
I'm trying to write a BASH script to output a partially completed command which I can then add parameters to, hit ENTER and then run. I want this to be implemented completely in BASH.
e.g.
~> ./test.sh
~> ls -al <CURSOR POSITION HERE>
The only variable I've found that's close is the PROMPT_COMMAND variable, which when set inside test.sh to 'ls -al', will then immediately execute it once the script has exited.
Is there a way to stop the immediate execution, so I can add, say, *.log?
How about
read -e -p"$PWD> " -i"ls -al " cmd; eval "$cmd"

What is $? var in script below on condition of while loop [duplicate]

This question already has answers here:
What is the $? (dollar question mark) variable in shell scripting? [duplicate]
(9 answers)
Closed 9 years ago.
#!/bin/sh
host google.com>/dev/null
while [ $? -ne 0 ];
do
sleep 3
done
say "You are now connected to internet"
I guess $? is associated with google.com>/dev/null, making the logic work, but i am interested in detail description on $?
Thanks in advance.
I know this will sound pedantic, but $? is not a variable, it is a value. ? is the name of the variable, placing $ at the front gives the value.
Now you can search for ? in man bash:
Expands to the exit status of the most recently executed foreground pipeline.
It is often tested unnecessarily. if and while statements in bash (and most shells) test for success(true) or failure(false). Success is where the value of ? is 0, and failure when it is some other number (which has the range 1-255). ! means "not", as in many languages, and inverts the truth:
while ! host google.com>/dev/null
do
sleep 3
done
echo "You are now connected to internet"
(I had to use echo, not sure where say comes from, Perl?)

Resources