Shell scripting question Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionionCube shell script gives errorShell script wrapper to prevent running command with no arguments?“too many arguments in [ (test) statement”shell calling another shell program fails when run by crontabshell script works fine when executed in terminal, but errors out when ran as concurrent programHow to loop through the files extracted by a find command and pass is as input to another command?getopts Unix inputOperand expected: syntax error (comparing statement)Shell script renameHow to get du -ksh working without a carriage return in shell-scripting?
How does light 'choose' between wave and particle behaviour?
Sentence order: Where to put もう
Would it be easier to apply for a UK visa if there is a host family to sponsor for you in going there?
Did any compiler fully use 80-bit floating point?
Is there a kind of relay that only consumes power when switching?
What's the meaning of "fortified infraction restraint"?
"Lost his faith in humanity in the trenches of Verdun" — last line of an SF story
Did Mueller's report provide an evidentiary basis for the claim of Russian govt election interference via social media?
What is Adi Shankara referring to when he says "He has Vajra marks on his feet"?
Can anything be seen from the center of the Boötes void? How dark would it be?
What is "gratricide"?
Find 108 by using 3,4,6
What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?
Time to Settle Down!
Belief In God or Knowledge Of God. Which is better?
How to unroll a parameter pack from right to left
Trademark violation for app?
Quadrilaterals with equal sides
How many time has Arya actually used Needle?
How to play a character with a disability or mental disorder without being offensive?
Take 2! Is this homebrew Lady of Pain warlock patron balanced?
Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode
AppleTVs create a chatty alternate WiFi network
Why weren't discrete x86 CPUs ever used in game hardware?
Shell scripting question
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionionCube shell script gives errorShell script wrapper to prevent running command with no arguments?“too many arguments in [ (test) statement”shell calling another shell program fails when run by crontabshell script works fine when executed in terminal, but errors out when ran as concurrent programHow to loop through the files extracted by a find command and pass is as input to another command?getopts Unix inputOperand expected: syntax error (comparing statement)Shell script renameHow to get du -ksh working without a carriage return in shell-scripting?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This Linux program is supposed to print 0 to 4. But I'm unable to find error in this code.
#!/bin/sh
a=0
while [ $a -lt 5 ]
do
echo $a
a='expr $a + 1'
done
When I try to run it, it says 'Line 5: [: too many arguments'.
shell-script
add a comment |
This Linux program is supposed to print 0 to 4. But I'm unable to find error in this code.
#!/bin/sh
a=0
while [ $a -lt 5 ]
do
echo $a
a='expr $a + 1'
done
When I try to run it, it says 'Line 5: [: too many arguments'.
shell-script
1
Please give your question a more descriptive title.
– phk
Oct 17 '16 at 17:55
add a comment |
This Linux program is supposed to print 0 to 4. But I'm unable to find error in this code.
#!/bin/sh
a=0
while [ $a -lt 5 ]
do
echo $a
a='expr $a + 1'
done
When I try to run it, it says 'Line 5: [: too many arguments'.
shell-script
This Linux program is supposed to print 0 to 4. But I'm unable to find error in this code.
#!/bin/sh
a=0
while [ $a -lt 5 ]
do
echo $a
a='expr $a + 1'
done
When I try to run it, it says 'Line 5: [: too many arguments'.
shell-script
shell-script
edited Apr 13 at 15:07
Rui F Ribeiro
42.2k1484142
42.2k1484142
asked Oct 17 '16 at 13:46
SparshSparsh
4
4
1
Please give your question a more descriptive title.
– phk
Oct 17 '16 at 17:55
add a comment |
1
Please give your question a more descriptive title.
– phk
Oct 17 '16 at 17:55
1
1
Please give your question a more descriptive title.
– phk
Oct 17 '16 at 17:55
Please give your question a more descriptive title.
– phk
Oct 17 '16 at 17:55
add a comment |
3 Answers
3
active
oldest
votes
You have use to backtick quotes for command substitution
a=`expr $a + 1`
5
Or better now adays is$(...)
notation
– Eric Renouf
Oct 17 '16 at 13:54
@Eric Renouf Is this portable?
– rudimeier
Oct 17 '16 at 13:57
1
Yes, it's specified as part of the POSIX shell command language: pubs.opengroup.org/onlinepubs/9699919799
– Eric Renouf
Oct 17 '16 at 14:02
2
stackoverflow.com/questions/4708549/… apparently yes, and$(...)
is better practice because it makes nesting clearer (whereas backticks need escaping for nesting).
– Valentin B.
Oct 17 '16 at 14:03
add a comment |
I've provided three examples, first two are with while
loops and the third is with an until
loop. The first example uses very close syntax to your original, the third is a bit more complex as it uses dynamic settings for counter maximum and an array for iterating though words (space separated arguments) passed one at a time. Now these are simple examples and intended to only answer your direct questions in the scripting language of Bash if you want more advanced examples see the link attached to a script I'm maintaining on GitHub for examples of nested loops among other tech-wizardry.
Example one
#!/usr/bin/env bash
let _counter=0
let _max="5"
while [ "$_counter" -lt "$_max" ]; do
echo "$_counter"
let _counter=++
done
unset _counter
unset _max
Running example one example
./example_one.sh
1
2
3
4
5
Example two
#!/usr/bin/env bash
_arr_args=( "$@" )
let _counter=0
let _max="$#_arr_args[@]"
while [ "$_counter" -lt "$_max" ]; do
echo "$_arr_args[$_counter]"
let _counter=++
done
unset _counter
unset _max
Running example two example
./example_two.sh "$(seq 1 5)"
## same output as example one
Example three (bonus)
#!/usr/bin/env bash
_arr_args=( "$@" )
let _counter=0
let _max="$#_arr_args[@]"
until [ "$_counter" = "$_max" ]; do
echo "$_arr_args[$_counter]"
let _counter=++
done
unset _counter
unset _max
Running example three example
./example_three.sh "$(seq 1 5)"
## same output as example one
Notes for new Bash script writers
- look up built-in variables/arrays and substitutions rules; it'll save you time, effort, and from unneeded piping to other programs.
- look up
shellcheck
it's available in most distributions, and in source form from it's authors, and is integrated into Code Climate with GitHub. - as eluded to above here is the link to an example of a script that makes use of both looping types shown above as well as auto code checking. However, the scripts' intention is well beyond the scope of answering the above question so search for "while" & "until" to find the relevant functions that use these calls.
bash
!=sh
there are similarities but they're different, personally I find bash to be the better choice because it's less likely to bugger-up if given a sh script where as shell will do odd things with bash syntax.
add a comment |
You are assigning the string 'expr $a + 1'
to a. That's not likely what you want. Just change single quotes to backticks and it will work:
a=`expr $a + 1`
The above should answer your question. Below is some additional thoughts:
If your /bin/sh
implementation is Bash, you could also consider using $(...)
instead of backticks. This has several advantages: it avoids typographic confusion between the single quote and the backtick, the $(...)
form can be nested, and each nesting is in its own quoting context (so you can do things such as:
variable="$(somecommand -o "$file")"
Here is another implementation:
#/bin/bash
declare -i a=0
while [[ $a -lt 5 ]]; do
echo $a
((a++))
done
Note that sh
is not an implementation. So, to answer your question safely, we'd need to know if your intention includes POSIX portability, or if you are interested in a particular implementation such as Bash
, dash
, tcsh
, etc.
$(...)
is POSIX, it will work in any POSIX shell (so the POSIXsh
of any system, most of the time found in/bin
), not justbash
$((...))
arithmetic expansion is also POSIX.
– Stéphane Chazelas
Oct 17 '16 at 14:09
@ValentinB.$(...)
is POSIX but not Bourne. Every system will have ash
where that works but not necessarily in/bin
like on Solaris 10 and older where/bin/sh
is the Bourne shell (an ancient, non-POSIX shell), and the standard shell is in/usr/xpg4/bin
– Stéphane Chazelas
Oct 17 '16 at 14:11
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f316968%2fshell-scripting-question%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You have use to backtick quotes for command substitution
a=`expr $a + 1`
5
Or better now adays is$(...)
notation
– Eric Renouf
Oct 17 '16 at 13:54
@Eric Renouf Is this portable?
– rudimeier
Oct 17 '16 at 13:57
1
Yes, it's specified as part of the POSIX shell command language: pubs.opengroup.org/onlinepubs/9699919799
– Eric Renouf
Oct 17 '16 at 14:02
2
stackoverflow.com/questions/4708549/… apparently yes, and$(...)
is better practice because it makes nesting clearer (whereas backticks need escaping for nesting).
– Valentin B.
Oct 17 '16 at 14:03
add a comment |
You have use to backtick quotes for command substitution
a=`expr $a + 1`
5
Or better now adays is$(...)
notation
– Eric Renouf
Oct 17 '16 at 13:54
@Eric Renouf Is this portable?
– rudimeier
Oct 17 '16 at 13:57
1
Yes, it's specified as part of the POSIX shell command language: pubs.opengroup.org/onlinepubs/9699919799
– Eric Renouf
Oct 17 '16 at 14:02
2
stackoverflow.com/questions/4708549/… apparently yes, and$(...)
is better practice because it makes nesting clearer (whereas backticks need escaping for nesting).
– Valentin B.
Oct 17 '16 at 14:03
add a comment |
You have use to backtick quotes for command substitution
a=`expr $a + 1`
You have use to backtick quotes for command substitution
a=`expr $a + 1`
edited Oct 17 '16 at 14:01
answered Oct 17 '16 at 13:51
rudimeierrudimeier
5,7321832
5,7321832
5
Or better now adays is$(...)
notation
– Eric Renouf
Oct 17 '16 at 13:54
@Eric Renouf Is this portable?
– rudimeier
Oct 17 '16 at 13:57
1
Yes, it's specified as part of the POSIX shell command language: pubs.opengroup.org/onlinepubs/9699919799
– Eric Renouf
Oct 17 '16 at 14:02
2
stackoverflow.com/questions/4708549/… apparently yes, and$(...)
is better practice because it makes nesting clearer (whereas backticks need escaping for nesting).
– Valentin B.
Oct 17 '16 at 14:03
add a comment |
5
Or better now adays is$(...)
notation
– Eric Renouf
Oct 17 '16 at 13:54
@Eric Renouf Is this portable?
– rudimeier
Oct 17 '16 at 13:57
1
Yes, it's specified as part of the POSIX shell command language: pubs.opengroup.org/onlinepubs/9699919799
– Eric Renouf
Oct 17 '16 at 14:02
2
stackoverflow.com/questions/4708549/… apparently yes, and$(...)
is better practice because it makes nesting clearer (whereas backticks need escaping for nesting).
– Valentin B.
Oct 17 '16 at 14:03
5
5
Or better now adays is
$(...)
notation– Eric Renouf
Oct 17 '16 at 13:54
Or better now adays is
$(...)
notation– Eric Renouf
Oct 17 '16 at 13:54
@Eric Renouf Is this portable?
– rudimeier
Oct 17 '16 at 13:57
@Eric Renouf Is this portable?
– rudimeier
Oct 17 '16 at 13:57
1
1
Yes, it's specified as part of the POSIX shell command language: pubs.opengroup.org/onlinepubs/9699919799
– Eric Renouf
Oct 17 '16 at 14:02
Yes, it's specified as part of the POSIX shell command language: pubs.opengroup.org/onlinepubs/9699919799
– Eric Renouf
Oct 17 '16 at 14:02
2
2
stackoverflow.com/questions/4708549/… apparently yes, and
$(...)
is better practice because it makes nesting clearer (whereas backticks need escaping for nesting).– Valentin B.
Oct 17 '16 at 14:03
stackoverflow.com/questions/4708549/… apparently yes, and
$(...)
is better practice because it makes nesting clearer (whereas backticks need escaping for nesting).– Valentin B.
Oct 17 '16 at 14:03
add a comment |
I've provided three examples, first two are with while
loops and the third is with an until
loop. The first example uses very close syntax to your original, the third is a bit more complex as it uses dynamic settings for counter maximum and an array for iterating though words (space separated arguments) passed one at a time. Now these are simple examples and intended to only answer your direct questions in the scripting language of Bash if you want more advanced examples see the link attached to a script I'm maintaining on GitHub for examples of nested loops among other tech-wizardry.
Example one
#!/usr/bin/env bash
let _counter=0
let _max="5"
while [ "$_counter" -lt "$_max" ]; do
echo "$_counter"
let _counter=++
done
unset _counter
unset _max
Running example one example
./example_one.sh
1
2
3
4
5
Example two
#!/usr/bin/env bash
_arr_args=( "$@" )
let _counter=0
let _max="$#_arr_args[@]"
while [ "$_counter" -lt "$_max" ]; do
echo "$_arr_args[$_counter]"
let _counter=++
done
unset _counter
unset _max
Running example two example
./example_two.sh "$(seq 1 5)"
## same output as example one
Example three (bonus)
#!/usr/bin/env bash
_arr_args=( "$@" )
let _counter=0
let _max="$#_arr_args[@]"
until [ "$_counter" = "$_max" ]; do
echo "$_arr_args[$_counter]"
let _counter=++
done
unset _counter
unset _max
Running example three example
./example_three.sh "$(seq 1 5)"
## same output as example one
Notes for new Bash script writers
- look up built-in variables/arrays and substitutions rules; it'll save you time, effort, and from unneeded piping to other programs.
- look up
shellcheck
it's available in most distributions, and in source form from it's authors, and is integrated into Code Climate with GitHub. - as eluded to above here is the link to an example of a script that makes use of both looping types shown above as well as auto code checking. However, the scripts' intention is well beyond the scope of answering the above question so search for "while" & "until" to find the relevant functions that use these calls.
bash
!=sh
there are similarities but they're different, personally I find bash to be the better choice because it's less likely to bugger-up if given a sh script where as shell will do odd things with bash syntax.
add a comment |
I've provided three examples, first two are with while
loops and the third is with an until
loop. The first example uses very close syntax to your original, the third is a bit more complex as it uses dynamic settings for counter maximum and an array for iterating though words (space separated arguments) passed one at a time. Now these are simple examples and intended to only answer your direct questions in the scripting language of Bash if you want more advanced examples see the link attached to a script I'm maintaining on GitHub for examples of nested loops among other tech-wizardry.
Example one
#!/usr/bin/env bash
let _counter=0
let _max="5"
while [ "$_counter" -lt "$_max" ]; do
echo "$_counter"
let _counter=++
done
unset _counter
unset _max
Running example one example
./example_one.sh
1
2
3
4
5
Example two
#!/usr/bin/env bash
_arr_args=( "$@" )
let _counter=0
let _max="$#_arr_args[@]"
while [ "$_counter" -lt "$_max" ]; do
echo "$_arr_args[$_counter]"
let _counter=++
done
unset _counter
unset _max
Running example two example
./example_two.sh "$(seq 1 5)"
## same output as example one
Example three (bonus)
#!/usr/bin/env bash
_arr_args=( "$@" )
let _counter=0
let _max="$#_arr_args[@]"
until [ "$_counter" = "$_max" ]; do
echo "$_arr_args[$_counter]"
let _counter=++
done
unset _counter
unset _max
Running example three example
./example_three.sh "$(seq 1 5)"
## same output as example one
Notes for new Bash script writers
- look up built-in variables/arrays and substitutions rules; it'll save you time, effort, and from unneeded piping to other programs.
- look up
shellcheck
it's available in most distributions, and in source form from it's authors, and is integrated into Code Climate with GitHub. - as eluded to above here is the link to an example of a script that makes use of both looping types shown above as well as auto code checking. However, the scripts' intention is well beyond the scope of answering the above question so search for "while" & "until" to find the relevant functions that use these calls.
bash
!=sh
there are similarities but they're different, personally I find bash to be the better choice because it's less likely to bugger-up if given a sh script where as shell will do odd things with bash syntax.
add a comment |
I've provided three examples, first two are with while
loops and the third is with an until
loop. The first example uses very close syntax to your original, the third is a bit more complex as it uses dynamic settings for counter maximum and an array for iterating though words (space separated arguments) passed one at a time. Now these are simple examples and intended to only answer your direct questions in the scripting language of Bash if you want more advanced examples see the link attached to a script I'm maintaining on GitHub for examples of nested loops among other tech-wizardry.
Example one
#!/usr/bin/env bash
let _counter=0
let _max="5"
while [ "$_counter" -lt "$_max" ]; do
echo "$_counter"
let _counter=++
done
unset _counter
unset _max
Running example one example
./example_one.sh
1
2
3
4
5
Example two
#!/usr/bin/env bash
_arr_args=( "$@" )
let _counter=0
let _max="$#_arr_args[@]"
while [ "$_counter" -lt "$_max" ]; do
echo "$_arr_args[$_counter]"
let _counter=++
done
unset _counter
unset _max
Running example two example
./example_two.sh "$(seq 1 5)"
## same output as example one
Example three (bonus)
#!/usr/bin/env bash
_arr_args=( "$@" )
let _counter=0
let _max="$#_arr_args[@]"
until [ "$_counter" = "$_max" ]; do
echo "$_arr_args[$_counter]"
let _counter=++
done
unset _counter
unset _max
Running example three example
./example_three.sh "$(seq 1 5)"
## same output as example one
Notes for new Bash script writers
- look up built-in variables/arrays and substitutions rules; it'll save you time, effort, and from unneeded piping to other programs.
- look up
shellcheck
it's available in most distributions, and in source form from it's authors, and is integrated into Code Climate with GitHub. - as eluded to above here is the link to an example of a script that makes use of both looping types shown above as well as auto code checking. However, the scripts' intention is well beyond the scope of answering the above question so search for "while" & "until" to find the relevant functions that use these calls.
bash
!=sh
there are similarities but they're different, personally I find bash to be the better choice because it's less likely to bugger-up if given a sh script where as shell will do odd things with bash syntax.
I've provided three examples, first two are with while
loops and the third is with an until
loop. The first example uses very close syntax to your original, the third is a bit more complex as it uses dynamic settings for counter maximum and an array for iterating though words (space separated arguments) passed one at a time. Now these are simple examples and intended to only answer your direct questions in the scripting language of Bash if you want more advanced examples see the link attached to a script I'm maintaining on GitHub for examples of nested loops among other tech-wizardry.
Example one
#!/usr/bin/env bash
let _counter=0
let _max="5"
while [ "$_counter" -lt "$_max" ]; do
echo "$_counter"
let _counter=++
done
unset _counter
unset _max
Running example one example
./example_one.sh
1
2
3
4
5
Example two
#!/usr/bin/env bash
_arr_args=( "$@" )
let _counter=0
let _max="$#_arr_args[@]"
while [ "$_counter" -lt "$_max" ]; do
echo "$_arr_args[$_counter]"
let _counter=++
done
unset _counter
unset _max
Running example two example
./example_two.sh "$(seq 1 5)"
## same output as example one
Example three (bonus)
#!/usr/bin/env bash
_arr_args=( "$@" )
let _counter=0
let _max="$#_arr_args[@]"
until [ "$_counter" = "$_max" ]; do
echo "$_arr_args[$_counter]"
let _counter=++
done
unset _counter
unset _max
Running example three example
./example_three.sh "$(seq 1 5)"
## same output as example one
Notes for new Bash script writers
- look up built-in variables/arrays and substitutions rules; it'll save you time, effort, and from unneeded piping to other programs.
- look up
shellcheck
it's available in most distributions, and in source form from it's authors, and is integrated into Code Climate with GitHub. - as eluded to above here is the link to an example of a script that makes use of both looping types shown above as well as auto code checking. However, the scripts' intention is well beyond the scope of answering the above question so search for "while" & "until" to find the relevant functions that use these calls.
bash
!=sh
there are similarities but they're different, personally I find bash to be the better choice because it's less likely to bugger-up if given a sh script where as shell will do odd things with bash syntax.
edited Apr 15 at 1:39
answered Oct 17 '16 at 14:43
S0AndS0S0AndS0
1967
1967
add a comment |
add a comment |
You are assigning the string 'expr $a + 1'
to a. That's not likely what you want. Just change single quotes to backticks and it will work:
a=`expr $a + 1`
The above should answer your question. Below is some additional thoughts:
If your /bin/sh
implementation is Bash, you could also consider using $(...)
instead of backticks. This has several advantages: it avoids typographic confusion between the single quote and the backtick, the $(...)
form can be nested, and each nesting is in its own quoting context (so you can do things such as:
variable="$(somecommand -o "$file")"
Here is another implementation:
#/bin/bash
declare -i a=0
while [[ $a -lt 5 ]]; do
echo $a
((a++))
done
Note that sh
is not an implementation. So, to answer your question safely, we'd need to know if your intention includes POSIX portability, or if you are interested in a particular implementation such as Bash
, dash
, tcsh
, etc.
$(...)
is POSIX, it will work in any POSIX shell (so the POSIXsh
of any system, most of the time found in/bin
), not justbash
$((...))
arithmetic expansion is also POSIX.
– Stéphane Chazelas
Oct 17 '16 at 14:09
@ValentinB.$(...)
is POSIX but not Bourne. Every system will have ash
where that works but not necessarily in/bin
like on Solaris 10 and older where/bin/sh
is the Bourne shell (an ancient, non-POSIX shell), and the standard shell is in/usr/xpg4/bin
– Stéphane Chazelas
Oct 17 '16 at 14:11
add a comment |
You are assigning the string 'expr $a + 1'
to a. That's not likely what you want. Just change single quotes to backticks and it will work:
a=`expr $a + 1`
The above should answer your question. Below is some additional thoughts:
If your /bin/sh
implementation is Bash, you could also consider using $(...)
instead of backticks. This has several advantages: it avoids typographic confusion between the single quote and the backtick, the $(...)
form can be nested, and each nesting is in its own quoting context (so you can do things such as:
variable="$(somecommand -o "$file")"
Here is another implementation:
#/bin/bash
declare -i a=0
while [[ $a -lt 5 ]]; do
echo $a
((a++))
done
Note that sh
is not an implementation. So, to answer your question safely, we'd need to know if your intention includes POSIX portability, or if you are interested in a particular implementation such as Bash
, dash
, tcsh
, etc.
$(...)
is POSIX, it will work in any POSIX shell (so the POSIXsh
of any system, most of the time found in/bin
), not justbash
$((...))
arithmetic expansion is also POSIX.
– Stéphane Chazelas
Oct 17 '16 at 14:09
@ValentinB.$(...)
is POSIX but not Bourne. Every system will have ash
where that works but not necessarily in/bin
like on Solaris 10 and older where/bin/sh
is the Bourne shell (an ancient, non-POSIX shell), and the standard shell is in/usr/xpg4/bin
– Stéphane Chazelas
Oct 17 '16 at 14:11
add a comment |
You are assigning the string 'expr $a + 1'
to a. That's not likely what you want. Just change single quotes to backticks and it will work:
a=`expr $a + 1`
The above should answer your question. Below is some additional thoughts:
If your /bin/sh
implementation is Bash, you could also consider using $(...)
instead of backticks. This has several advantages: it avoids typographic confusion between the single quote and the backtick, the $(...)
form can be nested, and each nesting is in its own quoting context (so you can do things such as:
variable="$(somecommand -o "$file")"
Here is another implementation:
#/bin/bash
declare -i a=0
while [[ $a -lt 5 ]]; do
echo $a
((a++))
done
Note that sh
is not an implementation. So, to answer your question safely, we'd need to know if your intention includes POSIX portability, or if you are interested in a particular implementation such as Bash
, dash
, tcsh
, etc.
You are assigning the string 'expr $a + 1'
to a. That's not likely what you want. Just change single quotes to backticks and it will work:
a=`expr $a + 1`
The above should answer your question. Below is some additional thoughts:
If your /bin/sh
implementation is Bash, you could also consider using $(...)
instead of backticks. This has several advantages: it avoids typographic confusion between the single quote and the backtick, the $(...)
form can be nested, and each nesting is in its own quoting context (so you can do things such as:
variable="$(somecommand -o "$file")"
Here is another implementation:
#/bin/bash
declare -i a=0
while [[ $a -lt 5 ]]; do
echo $a
((a++))
done
Note that sh
is not an implementation. So, to answer your question safely, we'd need to know if your intention includes POSIX portability, or if you are interested in a particular implementation such as Bash
, dash
, tcsh
, etc.
edited Oct 17 '16 at 14:10
answered Oct 17 '16 at 14:03
Steve AmerigeSteve Amerige
1518
1518
$(...)
is POSIX, it will work in any POSIX shell (so the POSIXsh
of any system, most of the time found in/bin
), not justbash
$((...))
arithmetic expansion is also POSIX.
– Stéphane Chazelas
Oct 17 '16 at 14:09
@ValentinB.$(...)
is POSIX but not Bourne. Every system will have ash
where that works but not necessarily in/bin
like on Solaris 10 and older where/bin/sh
is the Bourne shell (an ancient, non-POSIX shell), and the standard shell is in/usr/xpg4/bin
– Stéphane Chazelas
Oct 17 '16 at 14:11
add a comment |
$(...)
is POSIX, it will work in any POSIX shell (so the POSIXsh
of any system, most of the time found in/bin
), not justbash
$((...))
arithmetic expansion is also POSIX.
– Stéphane Chazelas
Oct 17 '16 at 14:09
@ValentinB.$(...)
is POSIX but not Bourne. Every system will have ash
where that works but not necessarily in/bin
like on Solaris 10 and older where/bin/sh
is the Bourne shell (an ancient, non-POSIX shell), and the standard shell is in/usr/xpg4/bin
– Stéphane Chazelas
Oct 17 '16 at 14:11
$(...)
is POSIX, it will work in any POSIX shell (so the POSIX sh
of any system, most of the time found in /bin
), not just bash
$((...))
arithmetic expansion is also POSIX.– Stéphane Chazelas
Oct 17 '16 at 14:09
$(...)
is POSIX, it will work in any POSIX shell (so the POSIX sh
of any system, most of the time found in /bin
), not just bash
$((...))
arithmetic expansion is also POSIX.– Stéphane Chazelas
Oct 17 '16 at 14:09
@ValentinB.
$(...)
is POSIX but not Bourne. Every system will have a sh
where that works but not necessarily in /bin
like on Solaris 10 and older where /bin/sh
is the Bourne shell (an ancient, non-POSIX shell), and the standard shell is in /usr/xpg4/bin
– Stéphane Chazelas
Oct 17 '16 at 14:11
@ValentinB.
$(...)
is POSIX but not Bourne. Every system will have a sh
where that works but not necessarily in /bin
like on Solaris 10 and older where /bin/sh
is the Bourne shell (an ancient, non-POSIX shell), and the standard shell is in /usr/xpg4/bin
– Stéphane Chazelas
Oct 17 '16 at 14:11
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f316968%2fshell-scripting-question%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please give your question a more descriptive title.
– phk
Oct 17 '16 at 17:55