0

I get that the second command is executed even when i != 1.

(( i == 1)) && florg="${fl%-*.png}.org" ; touch "$florg"

But want the two commands to execute just once.

1 Answer 1

2

Simplest thing to do is use a regular if statement:

if ((i == 1)); then
  florg="${fl%-*.png}.org"
  touch "$florg"
fi

Otherwise use braces to group the commands:

(( i == 1)) && { florg="${fl%-*.png}.org" ; touch "$florg"; }

See 3.2.5.3 Grouping Commands in the manual.

If you don't need to keep the variable, parentheses can be used (which executes the commands in a subshell). Parentheses are more forgiving about whitespace and semicolons:

((i == 1)) && (florg="${fl%-*.png}.org"; touch "$florg")
3
  • Looks like the last ; is really important.
    – Vera
    Oct 20, 2021 at 20:20
  • Yes. It's documented. Read the provided link. Oct 20, 2021 at 20:22
  • Not many people know that. You are right.
    – Vera
    Oct 20, 2021 at 20:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .