0

When I am running individual lines of the given bash script gives me output as I desired. But when I run it as a whole It shows a syntax error, I am a newcomer to this language please help me to figure out the problem. Thankyou

#!/bin/bash
col1=$( ls -l | head | awk '{print $1}')
echo $col1

col2=$(ls -l | head | awk '{print $2}')
echo $col2
paste <(echo "$col1") <(echo "$col2") | column --table --output-separator "|" | cat -s -n 

enter image description here

user96368
  • 83
  • 1
  • 1
  • 7
  • 1
    `sh` is not bash -- it's a separate shell with less syntax available. When you run `sh yourscript`, you disable bash-only syntax -- even on operating systems where sh is a symlink to bash, it runs in compatibility mode with fewer features when started under the `sh` name. – Charles Duffy Aug 03 '23 at 16:21
  • Use `./yourscript`, not `sh yourscript` _or_ `bash yourscript` -- that way the `#!/bin/bash` shebang at the top is honored to select an interpreter. – Charles Duffy Aug 03 '23 at 16:23
  • (btw, this is also part of why we encourage people not to use `.sh` extensions on filenames, _especially_ for scripts that aren't written for execution by sh; for a longer discussion of the topic, see [Commandname Extensions Considered Harmful](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/). – Charles Duffy Aug 03 '23 at 16:51
  • Great ! Thank you Charles, for exactly pointing out the problem. It worked – user96368 Aug 03 '23 at 19:41

0 Answers0