LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-26-2007, 02:00 PM   #1
extendedping
Member
 
Registered: Feb 2004
Posts: 210

Rep: Reputation: 30
my first shell script isn't working


ok first off I have no programming experience to don't laugh at me...I am reading a linux book and just attempted my first shell script..here is the exact copy

#!/bin/bash

echo "Please enter your first name"
read $firstname
echo "Hello $firstname I hope you are doing well"

so I run the script and put my name "brad" and expect it so say..."hello brad I hope you are doing well...but instead it says "hello I hope you are doing well"

now I know this is not rocket science..how the heck will I ever learn shell scripting if I can't make this work
 
Old 02-26-2007, 02:09 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
It's a slight mistake. The script should be:
Code:
#!/bin/bash

echo "Please enter your first name"
read firstname
echo "Hello $firstname I hope you are doing well"
Use the '$' prefix to substitute the contents of the variable. Omit the '$' to refer to the variable itself. In the read, you want the data to go into firstname. In the echo you want to display the contents of firstname.

EDIT:
As a side note, I would suggest getting into the habit of referring to variable contents with the ${} notation. For example:
Code:
echo "Hello ${firstname} I hope you are doing well"
It makes the code completely clear as to what variable is being substituted. It's also necessary in some other advanced techniques which I'm sure you'll learn about in due time

Last edited by Dark_Helmet; 02-26-2007 at 02:13 PM.
 
Old 02-26-2007, 02:10 PM   #3
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,692

Rep: Reputation: 45
read $firstname

drop the dollar sign there
 
Old 02-26-2007, 02:23 PM   #4
extendedping
Member
 
Registered: Feb 2004
Posts: 210

Original Poster
Rep: Reputation: 30
shebangs shebangs...it works now thank...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Backup shell script not working properly jamespetts Linux - Networking 2 07-15-2006 11:03 AM
Commands from shell script not working Grassie Coetzee Linux - Software 2 03-13-2005 02:31 PM
shell script not working sanjith11 Programming 2 07-07-2004 02:55 AM
Escape character not working in shell script philipz Programming 1 04-29-2004 09:58 AM
why my shell script is not working linuxlah Linux - General 4 08-01-2002 08:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration