6

When adding a one liner shell script in an answer, what format/style do you think is better ?

For one liner bash/sh/ksh/zsh/etc., I'm formatting it like this:

$ echo -e "#include <stdio.h>\nint main(int nbargs, char *args[]) {
> printf(\"Hello \\\n\");
> }" | gcc -pipe -x c - -Wl,-oess2

The prompt signs ($ and >) correctly reflect the shell behavior for a one liner, but it's a pain in the neck for copy & paste. From my reader's point of view, the prompt signs are useful, it permits quick script type identification. But if you want to test it, you'll have to manually discard theses signs.

Trying to address this, I've tested many approaches without results yet:

$ echo -e "#include <stdio.h>\nint main(int nbargs, char *args[]) {

> printf(\"Hello \\\n\");

> }" | gcc -pipe -x c - -Wl,-oess2

$ echo -e "#include <stdio.h>\nint main(int nbargs, char *args[]) { > printf("Hello \\n"); > }" | gcc -pipe -x c - -Wl,-oess2

EDIT: My current best result, not very aesthetic...

    $
    >
    >
    echo -e '#include <stdio.h>\nint main(int nbargs, char *args[]) {
    printf("Hello \n");
    }'

There are two other questions that are somewhat related to this question :

What is the preferred way to format a long one-liner in an answer? from @22degrees

Should code in examples be copy-paste-able? [closed] from @Rakete1111

These questions are more specifically about one liner and prompt signs, but could be applied also to very long pipelined lines in scripts for the aesthetic aspect.

Is there a way on Stack Overflow to format one liner scripts in a way that will not harm reading and testing or to copy & paste ?

(If not, could we have a formatting feature to address this ?)

3
  • 1
    even in natural speech it is hard, and it has rules for that, there maybe a possibility to beatify the code, but that looks more like Sisyphus work and a never ending story about what doesn't work. your first attempt is good, as it is redable and needs only a little work
    – nbk
    Commented Mar 13, 2021 at 17:06
  • @nbk I see your point, a feature for just dealing with secondary prompt might be "overkill". I will simply remove the auxiliary for now, thus keep the main prompt that will not harm copy & paste. Anyway, as one of the linked question states it, there's other cases where a formatting way to separate codes / commands from prompts / formatting signs should be very convenient.
    – Zilog80
    Commented Mar 13, 2021 at 17:44
  • @nbk In the case someone want to go further, it doesn't seem a Sisyphus work to me, as there is mainly 5 types of shells to deal with : Bourne shell/bash/ksh/dash : User prompt $, root prompt #, auxiliary prompt > zsh: User prompt %, root prompt #, auxiliary prompt > or pipe> tcsh/csh: User prompt >, root prompt #, auxiliary prompt ? vms/dcl: User prompt $, root prompt $, auxiliary prompt _ Windows batch : User prompt [drive]>, root prompt [drive]>, auxiliary prompt Plus?
    – Zilog80
    Commented Mar 13, 2021 at 17:49

1 Answer 1

8

If you are interested in easy copy-paste, the best approach would be:

echo -e "#include <stdio.h>\nint main(int nbargs, char *args[]) {
  printf(\"Hello \\\n\");
}" | gcc -pipe -x c - -Wl,-oess2

Markdown code:

```bash
echo -e "#include <stdio.h>\nint main(int nbargs, char *args[]) {
  printf(\"Hello \\\n\");
}" | gcc -pipe -x c - -Wl,-oess2
```

This way the reader copies the entire code-block and pastes it in his terminal to run it.

If you are keen on specifying the shell to be used, relying on the prompt symbols isn't very reliable, and can be easily missed. Just mention the shell in the text if it matters, or if it is conceivable to be run as a script, add the relevant hashbang:

#!/bin/bash
echo '#include <stdio.h>
      int main(int nbargs, char *args[]) {
        printf("Hello \n");
      }' | gcc -pipe -x c - -Wl,-oess2

(it can still be copied to the terminal, but the shebang won't be considered by the shell in that case)

And since we are playing with multiline bash commands, why not use a heredoc instead? This way the syntax is POSIX compliant, so it doesn't matter what shell the reader is using (piping is also compliant obviously, but echo -e isn't) :

gcc -pipe -x c - -Wl,-oess2 << 'EOF'
  #include <stdio.h>
  int main(int nbargs, char *args[]) {
    printf("Hello \n");
  }
EOF
5
  • 1
    Though you could reduce the number of escapes in the printf if you used single quotes instead of double quotes for the echo command :p
    – user000001
    Commented Mar 13, 2021 at 17:38
  • The question is about a way to keep the prompt signs, as they are useful to guess the related shell in one eye-shot. For the printf escapes, the double quotes way seems more adequate to me as it's the usual way for C code. Do you think it's worth altering it to better fit the one liner ?
    – Zilog80
    Commented Mar 13, 2021 at 17:55
  • 2
    @Zilog80 The prompt signs are not a good way to show what shell to use. Just mention the shell in the text. Regarding the quotes, yes, the echo command is bash code, not c, so you should take advantage of the available features to make the code more readable.
    – user000001
    Commented Mar 13, 2021 at 17:58
  • 1
    I am an old dude, used to old books / documentations on which one liner where formatted with prompt signs. But copy & paste convenience for readers should prime, so i'll remove the auxiliary prompt signs for now. It seems still useful to keep the main prompt signs as it directly distinguishes between user and root command in related cases. If you ok with it, i will not yet mark your answer as accepted, i'm curious to check for other points of view.
    – Zilog80
    Commented Mar 13, 2021 at 18:08
  • 1
    @Zilog80 I agree with the last point
    – user000001
    Commented Mar 13, 2021 at 18:10

You must log in to answer this question.

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