0

I am having trouble with for loops at the command line. Basically they appear to prompt for with a new line without exec.

  • $(for i in {1..5}; do echo $i; done) simply hangs, never execs

  • echo $(for i in {1..5}; do echo $i; done) produces a command substitution error.

Running bash --norc works. However lint shellcheck of .bashrc shows no problems.

Debugging with strace bash ~/.bashrc didnt produce anything noteworthy.

What should I check for next, to get a clean terminal working?

Thanks

bash version 4.3.11

5
  • What happens if you use a for loop without the $(...)? Commented Feb 2, 2018 at 0:30
  • Both of these are valid syntax, as your fresh shell confirms. What do you mean by "prompt for with a new line"? Is it the (by default) > prompt you get if you just type while and hit enter? Have you checked your aliases with alias? Commented Feb 2, 2018 at 0:30
  • 1
    In a "stock" shell, the first command should produce 1: command not found and the second should just output the numbers 1 through 5 on one line. Commented Feb 2, 2018 at 0:55
  • If you know it's your dotfiles messing something up, then using PS4=':${BASH_SOURCE}:$LINENO+' bash -x -l -i to log the commands they run (and which file and line every such command comes from) is a pretty good place to start. Commented Feb 2, 2018 at 1:03
  • That said, insofar as this is a question about diagnosing configuration of an interactive shell as opposed to a question about writing scripts, it's probably a better fit for Unix & Linux than StackOverflow. Commented Feb 2, 2018 at 1:05

1 Answer 1

1

Here's what I get when I enter those snippets on the command line:

prompt> $(for i in {1..5}; do echo $i; done)
1: command not found

prompt> echo $(for i in {1..5}; do echo $i; done)
1 2 3 4 5

In the first case, the syntax itself is valid, but since you don't have any command or variable assignment, bash is just doing the command substitution, and then attempting to parse the output of the command substitution as a command. It's exactly as if you'd done this:

prompt> 1 2 3 4 5
1: command not found

Since you're just getting a hang on this command, and no output, then there must be something else going on that's most likely not related to bash itself.

In the second case, for me anyway, it works. The numbers get echoed out on a single line because you didn't quote the command substitution, so the newlines are just treated as delimiters, and it's essentially identical to:

echo 1 2 3 4 5

If you quote the command substitution, you get this:

prompt> echo "$(for i in {1..5}; do echo $i; done)"
1
2
3
4
5

Bottom line: Your loop syntax is valid, as is using it inside a command substitution. Just don't expect the loop's output to be interpreted as a valid bash command. That is, unless you actually have a an executable named 1 in your PATH.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, it was an error in the .bashrc. Isolated the problem by going through the file line-by-line. Even shellcheck did not detect this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.