0

I have a simple bash script for file listing:

$ cat process.sh
for i in *; do echo $i; done
$

and then I run:

$ ./process.sh
a
b
c
d
process.sh
$

and

$ . ./process.sh










$

and

$ for i in *; do echo $i; done










$

I've read Why does Bash behave differently when called as sh? which explains that inline commands use sh instead of bash - is wildcard non-POSIX in this case?

  • Why do I get different behaviours when executing the same code?
  • How to make this example work?
  • Are there any other cases to look out for?
Community
  • 1
  • 1
Sebastian Kramer
  • 689
  • 1
  • 9
  • 18
  • 1
    I cannot reproduce the empty output you see; it correctly produces a file listing with both direct execution and sourcing several shells, include `bash` (3.2 and 4.3), `zsh`, and `dash`. – chepner Mar 29 '15 at 18:49
  • Can you confirm that that you have five files in the directory, named `a`-`d` and `process.sh`, and that sourcing the posted `process.sh` file results in ten blank lines? What does `ps -p $$` say? – that other guy Mar 29 '15 at 19:02
  • What do you have `echo` aliassed to in your shell environment? (Run `alias` with no arguments and look at the output.) – Jonathan Leffler Mar 29 '15 at 19:03
  • 1. I have files a-d and the script in the directory 2. $ ps -p $$ PID TTY TIME CMD 21858 pts/1 00:00:00 bash 3. I don't have any alias for echo command. 4. `source process.sh` produces empty lines as well I'm using vi editing mode? Does this have anything to do? – Sebastian Kramer Mar 29 '15 at 19:33
  • Ok - solved it. There was a weird `for` alias in bashrc. Thanks for the effort everyone. – Sebastian Kramer Mar 29 '15 at 19:37
  • Don't alias keywords. If you think you must alias keywords, think again. If you still think you must alias keywords, don't ask about why things don't work. – Jonathan Leffler Mar 29 '15 at 19:57
  • Noted. Thanks for the tip! – Sebastian Kramer Mar 29 '15 at 20:03

1 Answers1

0

Solved. The reason was an incorrect for alias. Thanks for your replies.

Sebastian Kramer
  • 689
  • 1
  • 9
  • 18