linux-admin.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* bash arithmetic
@ 2003-02-27 10:00 urgrue
  2003-02-27 10:22 ` Carl
  2003-02-27 14:35 ` Jeff Largent
  0 siblings, 2 replies; 3+ messages in thread
From: urgrue @ 2003-02-27 10:00 UTC (permalink / raw)
  To: linux-admin

simple problem:
NAME=john
echo name is $NAME
echo now im gonna $NAME=$(($NAME+1))
$NAME=$(($NAME+1))

returns:
name is john
now im gonna john=1
./testscript: john=1: command not found

but whats wrong with john=1? i want to set that variable. i can put 
john=1 in the script (in that form) and it works fine. 
any suggestions?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: bash arithmetic
  2003-02-27 10:00 bash arithmetic urgrue
@ 2003-02-27 10:22 ` Carl
  2003-02-27 14:35 ` Jeff Largent
  1 sibling, 0 replies; 3+ messages in thread
From: Carl @ 2003-02-27 10:22 UTC (permalink / raw)
  To: urgrue, linux-admin

At 12:00 27/02/2003 +0200, urgrue wrote:
>simple problem:
>NAME=john
>echo name is $NAME
>echo now im gonna $NAME=$(($NAME+1))
>$NAME=$(($NAME+1))

Should this last line be a variable assignment or a command execution?

If it is a variable assignment then you need to drop the $ at the beginning of the line else
it is calling the variable for command execution hence the error "john=1" not found.
You might want to use expr for arithmetic in bash as it is easier to read.

Should be:

NAME=john
echo name is $NAME
echo now im gonna $NAME=$(($NAME+1))
NAME=$(($NAME+1))                                       # no $ at start of line
echo $NAME

Which returns:

[carl@dev carl]$. ./test.sh
name is john
now im gonna john=1
1


>returns:
>name is john
>now im gonna john=1
>./testscript: john=1: command not found
>
>but whats wrong with john=1? i want to set that variable. i can put 
>john=1 in the script (in that form) and it works fine. 
>any suggestions?
>-
>To unsubscribe from this list: send the line "unsubscribe linux-admin" in
>the body of a message to majordomo@vger.kernel.org
>More majordomo info at  http://vger.kernel.org/majordomo-info.html 

--
Carl



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: bash arithmetic
  2003-02-27 10:00 bash arithmetic urgrue
  2003-02-27 10:22 ` Carl
@ 2003-02-27 14:35 ` Jeff Largent
  1 sibling, 0 replies; 3+ messages in thread
From: Jeff Largent @ 2003-02-27 14:35 UTC (permalink / raw)
  To: linux-admin

Try it this way,

NAME=john
echo name is $NAME
echo now im gonna $NAME=$(($NAME+1))
eval ${NAME}=$(($NAME+1))
echo "john = $john"


urgrue wrote:
> simple problem:
> NAME=john
> echo name is $NAME
> echo now im gonna $NAME=$(($NAME+1))
> $NAME=$(($NAME+1))
> 
> returns:
> name is john
> now im gonna john=1
> ./testscript: john=1: command not found
> 
> but whats wrong with john=1? i want to set that variable. i can put 
> john=1 in the script (in that form) and it works fine. 
> any suggestions?
> -
> To unsubscribe from this list: send the line "unsubscribe linux-admin" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
> 

-- 
Jeff Largent                   ImageLinks, Inc.
Sr System Admin                Melbourne, Fl 32935
(321) 253-0011                 fax: (321) 253-5559



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-02-27 14:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-02-27 10:00 bash arithmetic urgrue
2003-02-27 10:22 ` Carl
2003-02-27 14:35 ` Jeff Largent

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).