Find the answer to your Linux question:
Results 1 to 8 of 8
hi two questions 1] can a variable name begin with a $ . if no; then no then skip the code and the goto question 2] Code: #!/bin/bash echo "hi ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Mar 2005
    Location
    Bangalore, INDIA
    Posts
    122

    variable name in a shell script


    hi
    two questions
    1]
    can a variable name begin with a $ .
    if no; then no then skip the code and the goto question 2]

    Code:
    #!/bin/bash
    echo "hi there"
    echo "whats ur name"
    read $name
    echo "ur name is  $$name"
    how do imake echo print the name

    2]
    in c or c++ almost every line ends with a semicolon (except the loops)
    what abt shell scrits ?????any particular rule........
    any good book on only shell programmingin paticular
    link plz
    thank you
    Portability is for people who cannot programme

  2. #2
    Linux Newbie
    Join Date
    Jul 2004
    Location
    Calcutta, India
    Posts
    220
    1st Q : I guess the answer is obviously NO.

    2nd Q : Cant tell you much about links, but in BASH scripts you dont have to
    use ";" at the end of each line, you can only use ";" when you are typing
    multiple commands in a single line. For eg. :
    Code:
    if test $# -ne 2
    then
    can also be written as
    Code:
    if test $# -ne 2 ; then
    I hope I am clear ...
    [/code]

  3. #3
    Linux Newbie
    Join Date
    Mar 2005
    Location
    Bangalore, INDIA
    Posts
    122
    one more question
    Code:
    #!bin/bash
    n=1
    while [ $n -le 10 ]
    do echo $n
    n=$[n+1]
    done
    now i didn't get the line n=$[n+1]
    i mean what is the difference btw n and $n
    i wrote that line as $n=$[n+1]
    the programme went into an infinite loop..........
    what's the difference
    pllz xplain
    thank you
    Portability is for people who cannot programme

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790
    in bash when u wanna set a variable u dont use the $, but when u call it u do.
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  6. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Lets's say n has the value 1.So when you write
    $n=[$n+1] the shell substitutes the value of n which gives
    1=2 and it tells you that there is no command with
    this name.But if you write n=[$n+1] then it means
    that the variable on the left (n) must take the value on the
    right which is 2.

  7. #6
    Linux Newbie
    Join Date
    Mar 2005
    Location
    Bangalore, INDIA
    Posts
    122
    thanks to both of you
    u can bet i got the difference now..........
    Portability is for people who cannot programme

  8. #7
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790


    It confused me when i was startin a little bit on scripting aswell.... i'm used to PHP where you WOULD do:
    Code:
    $n = $n+1;
    Oh well, each to their own! lol
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  9. #8
    Linux Engineer Giro's Avatar
    Join Date
    Jul 2003
    Location
    England
    Posts
    1,219
    Quote Originally Posted by prosen
    1st Q : I guess the answer is obviously NO.
    It depends when declaring a variable you omit the $ eg.. MYVAR but when accessing it you use $MYVAR or ${MYVAR} so in a sense you so use $

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •