'&&' vs. '&' with the 'test' command in Bash
What is & in Bash? And a single & means that the preceding commands—to the immediate left of the &—should simply be run in the background.,The meaning of && and & are intrinsically different.,What is && in Bash? In Bash—and many other programming languages—&& means “AND”. And in command execution context like this, it means items to the left as well as right of && should be run in sequence in this case.,Since the second command is just running test -x examples.desktop in the background it happens quite quickly, so the process ID is spawned and gone pretty immediately.
So looking at your example:
gndlp @ubuntu: ~$ test - x examples.desktop && echo $ ? gndlp @ubuntu : ~$ test - x examples.desktop & echo $ ?  2992 0
command2 will execute if command1 has executed successfully. This operator allows us to check the exit status of command1.,The second command will only execute if the first command has executed successfully i.e, its exit status is zero. This operator can be used to check if the first command has been successfully executed. This is one of the most used commands in the command line.,The execution of the second command is independent of the exit status of the first command. If the first command does not get successfully executed, then also the second command will get executed.,Even if the exit status of the first command is 1 i.e, b already exists, the second command will get executed and b will be again initialized with a new value.
command1 && command2
"&&" is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors (or, more accurately, exits with a return code of 0)., Stack Exchange network consists of 178 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. ,"-" is a command line argument with no specific bash function. Exactly how it is handled is dependent on the command being run (in this case apt-key). Depending on context, it's typically used to indicate either "read data from stdin rather than from a file", or "process the remainder of the line as data rather than as command line arguments".,&& Can also be used inside of the built-in test command in bash [[ ]] to combine expressions in a similar fashion as combining commands. The entire test operation will only be true if both operands (sides) of && are true, for example:
"\" by itself at the end of a line is a means of concatenating lines together. So the following two lines:
gpg--keyserver keyserver.ubuntu.com--recv 26 C2E075 && \ gpg-- export --armor 26 C2E075
are processed exactly the same as if the line was written as the single line:
gpg--keyserver keyserver.ubuntu.com--recv 26 C2E075 && gpg-- export --armor 26 C2E075
The double brackets allow you to put the && and || between the tests themselves. This is a newer syntax and may not be supported by every shell.,The && and || operators can be useful in a number of ways and, hopefully, are a little less mysterious than they might have seemed.,The Unix shell’s && and || operators provide some very useful functionality, but they can be a bit mysterious, especially considering the number of options for how they are used.,This script will output a "c" twice if the first argument ia "a" or if the second is "b". It will do the same thing if the first two arguments are "a" and "b".
#!/bin/bash echo $1 $2 if [$1 == "a"] && [$2 == "b"] then echo c fi if [ [$1 == "a" && $2 == "b"] ] then echo c fi
Is my PHP login system following best practices? Is the code really OOP? ,I am trying to understand how the logical operator precedence works in bash. For example, I would have expected, that the following command does not echo anything., Unix & Linux help chat ,Making statements based on opinion; back them up with references or personal experience.
So what happens in your example is that the leftmost operation (
||) is carried out first:
true || echo aaa
true is obviously true, the
|| operator short-circuits and the whole statement is considered true without the need to evaluate
echo aaa as you would expect. Now it remains to do the rightmost operation:
(...) && echo bbb
Since the first operation evaluated to true (i.e. had a 0 exit status), it's as if you're executing
true && echo bbb
You would get the same behavior with
false && echo aaa || echo bbb
It seems that in C and C-like languages
&& has higher precedence than
|| which is probably why you expected your original construct to behave like
true || (echo aaa && echo bbb).
The TEST-COMMAND list is executed, and if its return status is zero, the CONSEQUENT-COMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.,The following example demonstrates that TEST-COMMANDS might be any UNIX command that returns an exit status, and that if again returns an exit status of zero:,The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation. Just like the if is closed with fi, the opening square bracket should be closed after the conditions have been listed.,See the info pages for Bash for more information on pattern matching with the "(( EXPRESSION ))" and "[[ EXPRESSION ]]" constructs.
anny~ > cat msgcheck.sh #!/bin/bash echo "This scripts checks the existence of the messages file." echo "Checking..." if [-f / var / log / messages ] then echo "/var/log/messages exists." fi echo echo "...done." anny~ > . / msgcheck.sh This scripts checks the existence of the messages file. Checking... /var/log / messages exists. ...done.
There must be spaces between operators and expressions. For example, 2+2 is not correct; it should be written as 2 + 2.,Unix / Linux - Basic Operators,Unix / Linux - Shell Loops,Unix / Linux - Shell Functions
#!/bin/sh val = `expr 2 + 2` echo "Total value : $val"
The most obvious situation where you might need to quote a string is with the echo command, which just takes its arguments and prints them to the standard output. What is the point of this? As you will see in later chapters, the shell does quite a bit of processing on command lines—most of which involves some of the special characters listed in Table 1.6. echo is a way of making the result of that processing available on the standard output.,Here is a more practical example of quoting special characters. A few UNIX commands take arguments that often include wildcard characters, which need to be escaped so the shell doesn’t process them first. The most common such command is find, which searches for files throughout entire directory trees., This should also teach you something about the flexibility of placing I/O redirectors anywhere on the command line—even in places where they don’t seem to make sense.,the result is the string, taken literally. You needn’t quote the entire line, just the portion containing special characters (or characters you think might be special, if you just want to be sure):
Other "command-undefined" queries related to "'&&' vs. '&' with the 'test' command in Bash"
- Ref from useRef is null from Jest & react-testing-library unit test
- How to test a React app on a Ipad's ios environment from osx?
- A request to test the responsiveness of the React application [closed]
- React: useEffect vs useMemo vs useState
- React tests - UnhandledPromiseRejection
- How do you properly modify the state and fetch calls in a react component in a test?
- @fluentui\react-northstar Dropdown error when React Testing Library tries to click
- React + Jest: Test that setTimeOut didn't get called
- Running a component test in React is compiling my index.ts file and breaks the test
- React tests with IndexedDB
- Binding vs arrow-function (for react onClick event)
- Vs code takes long time to load variables in the first launch in React Native DEBUG mode
- Error while creating app using command prompt
- React Native showing error while making a build in iOS - Build failed The following build commands failed: CompileC
- Min-width: none vs unset what is the proper apprach?
- How can I mock only a function inside an component when tests are running in React?
- Why are my firebase emulator get-call promises not resolving in jest unit tests
- How do I test this function?
- Error: Invariant failed: You should not use <Link> outside a <Router> when I try to test my app using localhost
- Difference between providing a boolean vs object to historyApiFallback in webpack config?
- VSCode TypeScript JSX attribute autocomplete not working
- Catch error after promise initialize warning in unit test
- Mocking file input in React TestUtils
- Handle webpack loader syntax with Jest testing: exclamation raw-loader
- MapView.Polyline in react native with expo throws an error (I'm testing with an android)
- How to catch undefined functions with set_error_handler in PHP
- Installer/packager for a Java application for Ubuntu and SuSE
- How to remove text between tags in php?
- /tmp vs. /dev/shm for temp file storage on Linux?
- How to debug gettext not working in PHP?
- How to make strtotime parse dates in Australian (i.e. UK) format: dd/mm/yyyy?
- Get filesystem path of installed composer package
- PHP Manager - Downloaded file failed signature verification
- What methods exist for setting up a large email notification system?
- PHP Cut String At specific character
- How to implement authorization using a Telegram API?
- Faceted Search (solr) vs Good old filtering via PHP?
- What is meant by whereRaw in PHP Laravel framework
- WordPress and Call to undefined function add_menu_page()
- How to override trait function and call it from the overridden function?
- How to use multiple databases in Laravel
- How can I check if mysql table column even exists?
- Zend Framework cookie management
- How secure are PHP sessions?
- How can we open a link in private browsing mode