Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Tips for golfing in Bash

What general tips do you have for golfing in Bash? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Bash (e.g. "remove comments" is not an answer). Please post one tip per answer.

Answer*

Draft saved
Draft discarded
Cancel
5
  • 12
    \$\begingroup\$ Sometimes $( ) is needed if you have nested command substitutions; otherwise you'd have to escape the inner `` \$\endgroup\$ Commented Apr 13, 2014 at 4:18
  • 1
    \$\begingroup\$ These technically do different things, I've had to use the backticks instead of $() when I wanted to run the substitution on my machine instead of the scp target machine, for example. In most cases they're identical. \$\endgroup\$ Commented Apr 21, 2014 at 19:31
  • 4
    \$\begingroup\$ @undergroundmonorail: you never need backticks. Anything they can do, $() can do if you quote things properly. (unless you need your command to survive something that munges $ but not backticks). There are some subtle differences in quoting things inside them. mywiki.wooledge.org/BashFAQ/082 explains some differences. Unless you're golfing, never use backticks. \$\endgroup\$ Commented Sep 24, 2015 at 7:09
  • \$\begingroup\$ @PeterCordes I'm sure there was a way but everything I tried at the time didn't work. Even if backticks weren't the best solution, I was glad I knew about them because it was the only solution I had. ¯\_(ツ)_/¯ \$\endgroup\$ Commented Sep 24, 2015 at 7:43
  • \$\begingroup\$ @DigitalTrauma Sample of nesting: echo `bc <<<"\`date +%s\`-12"` ... (It's hard to post sample containing backtick in comment, there! ;) \$\endgroup\$ Commented Jan 24, 2017 at 17:22

AltStyle によって変換されたページ (->オリジナル) /