简体   繁体   中英

How to execute two BASH shell commands from a single test?

Why does this not work?

test $? -eq 1 || $(echo "hello"; echo "hi")

I get the following error:

Command 'hello' not found, but can be installed with:
snap install hello              # version 2.10, or
apt  install hello              # version 2.10-2ubuntu4
apt  install hello-traditional  # version 2.10-5
See 'snap info hello' for additional versions.

You're looking for 3.2.5.3 Grouping Commands

test $? -eq 1 || { echo "hello"; echo "hi"; }

The spaces around the braces and the trailing semicolon (or newline) are required.


However, don't test the exit status with the $? variable (it's too easy to accidentally insert another command in there)

Use the command directly

some command here && { echo hello; echo hi; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM