4 ms·
In scripts, I usually do stuff like this command \ --longopt1 \ --longopt2 arg \ argument or command | \ command 2 | \ command 3 \
by m463 6y ago
In scripts, I usually do stuff like this
command \
--longopt1 \
--longopt2 arg \
argument
or
command | \
command 2 | \
command 3 \
--opt1 \
--opt2 \
arg
- 52-6F-62 6y agoWow. I just realized I'm terrible for this. I usually format my programming pretty verbosely to make it future-readable but I'm terrible for not doing that in my scripts. I'll add a comment or something explaining it but now I feel guilty and should probably go back and rewrite a few lines... I mean—for long scripts I'll break them up but I haven't paid much mind to arguments/flags.
- dmurray 6y agoI do this if I have to look up the right flag to use. If it's something commonly seen like "grep -o" or "tar -xvzf" I assume whoever is maintaining the script (probably future me) will be more familiar with the abbreviations than the long versions.
- 52-6F-62 6y agoYeah good point. It did strike me, though, because I'd just written several scripts running JMeter commands and sort of breezed over how they're written even though I had to go and look up all the flags.
- viseztrance 6y agoIf you have a very long command you can run ctrl x e (hold control press x, than e) and edit it in your $EDITOR.
- InvaderFizz 6y agoI wish I had known this 20 years ago. Thanks!
- inshadows 6y agoIt's nice but you cannot comment individual lines. For that you can go one step further and use arrays: cmd=( command -x -y --bar OPTARG ARG ARG ) $cmd This works in Zsh. In Bash that would be ${cmd[@]} I think. I use this with long qemu command lines where I often modify and comment out arguments during testing.
- periodontal 6y agoFor bash, add quotes: "${cmd[@]}" Otherwise, you'll get word splitting. E.g., cmd=( test "!= !=" != "" ) "${cmd[@]}" && echo Success ${cmd[@]} || echo Failure
- tdons 6y agoYou can comment using a hack; check this out: command `# a comment` \ --longopt1 `# another comment` \ --longopt2 arg \ argument
- inshadows 6y agoI didn't mean commenting arguments for documentation but commenting them out while you're writing and testing the script. I.e. this won't work: command `# a comment` \ # --longopt1 \ --longopt2 arg \ argument AFAIK there's no hack for this. EDIT: Oh, `# a comment`. Didn't notice that. I'm going to explore it, thanks.
- lhoursquentin 6y agoYou should probably note that this hack spawns an additional shell process per comment :)
- BrandonM 6y agoYou don't even need the \ if the | is the last character of the line.
- deleted 6y ago[deleted]
- kl4m 6y agoDownside: forgetting a \ will truncate the flags list, leading to unexpected behaviour followed by an error.
- GordonS 6y agoThis is how I always do it do - I find it much more readable, and it leads to better diffs too.
- coldacid 6y agoIf I'm splitting across lines, I prefer to put the pipe _after_ the newline. Otherwise, I like the cut of your jib, and will likely do this with arguments in scripts from now on.
- m463 6y agosometimes I do something like this: foo \ --group1 \ \ --group 2 \ --more-stuff 2 \ | \ bar \ --bar-stuff using a line with a single backlash or breaking out the pipe can make grouping stuff clear. well, I mean as clear as having to use backslashes which to me is a little inelegant. Personally python and its indenting strategy has grown on me especially since editor support makes it painless to use and visually excellent.