0
for i in {1..4}; do echo "Welcome $i";done

I want

Welcome 1
Welcome 2
.
.

Instead I get

welcome {1..4}

Android terminal emulator

3

You are not running bash, or you're running an older version of bash, or a shell that doesn't understands brace expansions.

The most probable explanation is that your terminal emulator is running /bin/sh or some smaller shell (which is hopefully still mostly POSIX compliant).

In bash (and any other shell that supports brace expansions), your explicit for-loop may be changed into an implicit loop:

printf 'Welcome %d\n' {1..4}

or

printf 'Welcome %d\n' $(seq 4)

if you don't have brace expansion but do have seq.

  • brace expansion usually refers to the {x,y} csh feature from the late 70s (and added to ksh and many other Korn-like shells in the 80s). {1..4} is a zsh extension (from 1995) added to some other shells much later (in the 2000s, 2010s). – Stéphane Chazelas Jun 8 '17 at 9:09
  • Sorry, while brace expansion was added to bash and zsh from the start (1989, 1990) to pdksh in 1992, it was never added to ksh88. I don't know when it was added to ksh93, possibly from the start as ksh93d (1995) had it already. – Stéphane Chazelas Jun 8 '17 at 20:30
3

You are running in /bin/sh (shell), not /bin/bash.

$ /bin/sh -c 'for i in {1..4}; do echo "Welcome $i";done'
Welcome {1..4}

Now try with bash:

$ /bin/bash -c 'for i in {1..4}; do echo "Welcome $i";done'
Welcome 1
Welcome 2
Welcome 3
Welcome 4

If you prefer shell then do as Nikolas write:

for i in $(seq 4); do echo "Welcome $i" ; done

but I would suggest to use Bash.

  • The path doesn't matter. When running as /bin/sh, bash or ksh or zsh still expand {1..4}. The point would be that /bin/sh on that system is probably not based on zsh, ksh or bash but maybe on the Almquist shell (which doesn't have the feature) or yash where the feature is not enabled by default. In any case, that feature should not be used in sh scripts as that's not a standard sh feature, so the recommendation to use bash explicitly is certainly a good one. – Stéphane Chazelas Jun 8 '17 at 9:13
1

As Kusalananda said, either you're not running bash or the version is not very recent. I've tested the following in older versions of bash (maybe before it formed its idiom?) and it works with sh too:

for i in `seq 1 4`;do echo "Welcome $i";done

That should do the trick.

  • 2
    That {1..4} zsh feature was added to bash in 3.0. It's possible the OP uses a Solaris 8 or 9 system for instance that would come with an older version (IIRC; in an optional package). – Stéphane Chazelas Jun 8 '17 at 8:40
  • @StéphaneChazelas OP uses Android terminal emulator, therefore he uses Android. Link to this app: play.google.com/store/apps/… – MiniMax Jun 8 '17 at 8:52
1

If you use this app: Android terminal emulator, "then it is a terminal emulator for communicating with the built-in Android shell". That is, if you didn't install your own version of bash manually (your device should be rooted for this. Were you rooting your device?), then you have default Android shell, not bash. Therefore, you can use only Android's shell feature set, and can't use advanced bash syntax and features, for example: {1..4}.

Some information, which I found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.