Solved

syntax for assigning values to a variable

Posted on 2008-06-24
8
1,050 Views
Last Modified: 2013-12-26
very simple question, in a KornShell script, I want to assign a value to a variable, do I use single quote, double-quote, or what? What is the rule?

these two work, so no quotes at all
LOAD_TYPE=pde$1cov_mapd

trace_flag=FALSE - is this the boolean FALSE (aka reserved word), so it doesn't need quotes?

proposed:      
PLAN_TYPE=mapd - does this need quotes?

USER_FRIENDLY_PGM=MA-PD - does this need quotes, oftentimes the dash causes hiccups

USER_FRIENDLY_PGM=Med D - seems like this needs quotes since there is a space

0
Comment
Question by:Alaska Cowboy
  • 4
  • 3
8 Comments
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 21857674
Also, in this statement (which works in Production), why does ${LOAD_TYPE} have double-quotes?

Works:   if [ "${LOAD_TYPE}" != "pde41cov" ] && [ "${LOAD_TYPE}" != "pde43cov" ]

Would this work (no quotes on variable): if [ ${LOAD_TYPE} != pde41cov ] &&
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 50 total points
ID: 21858168
if [ "${LOAD_TYPE}" != "pde41cov" ]
would need " if you needed quotes for something like
LOAD_TYPE="a = b"
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 21858948
ok, sounds like double quotes are needed if there are spaces or special characters.

so is it true both of these work

LOAD_TYPE=mapd
LOAD_TYPE="mapd"

Thanks.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 48

Accepted Solution

by:
Tintin earned 75 total points
ID: 21860457
Rules are:

1.  Use single quotes when you want to assign a literal value to a variable.
2.  Use double quotes when if the value is blank, has spaces or a variable name
3.  No quotes are needed if assigning a string or variable that has no spaces.

0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 21861618
Tintin, excellent, thanks.
0
 
LVL 84

Expert Comment

by:ozo
ID: 21861855
3.  No quotes are needed if assigning a string or variable that has no spaces or quotes or $ or * or ? or [] or {} or \
0
 
LVL 1

Author Comment

by:Alaska Cowboy
ID: 21861873
ozo, ok, thanks for the clarification
0
 
LVL 84

Expert Comment

by:ozo
ID: 21861905
or < or > or ; or & or | or ( or )
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question