Solved

syntax for assigning values to a variable

Posted on 2008-06-24
8
1,052 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

690 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