Sponsored Content
Top Forums UNIX for Advanced & Expert Users Detecting unused variables... Post 302975417 by Don Cragun on Sunday 12th of June 2016 09:47:53 PM
Old 06-12-2016
Hi wisecracker,
There are lots of things to consider here. Strings used in variable assignments follow slightly different rules than strings used in other shell operations (such as command-line arguments to utilities). See the following script for example:
Code:
#!/bin/sh
a=" after"
b="before "
m=" middle "
x=' a b '
y=' echo d '
unset v1 v2
# Note that the expandsion of $y and $x in the next statement are not quoted.
v1="before"$y" "$x"after"
printf '"v1":X%sX\n' "$v1"
printf 'v1:X%sX\n' $v1

# Note that the strings to which $y and $x are substituted here, but not as
# variable expansions.  Here "echo" becomes a command to execute with v2 set as
# a variable in the environment of the echo command, but it is not set in the
# current shell execution environment.
v2="before" echo d " " a b "after"
printf '"v2":X%sX\n' "$v2"
printf 'v2:X%sX\n' $v2

# Now see how those same sets of strings are handled when passed as arguments to
# a utility instead of being assigned to a variable...
printf 'v1string:X%sX\n' "before"$y" "$x"after"
printf 'v2string:X%sX\n' "before" echo d " " a b "after"

# Spaces still matter when variable expansions are separated by IFS characters.
set -xv
w=$b $m $a
z=$b$m$a
set +xv
# And, of course leading and trailing spaces disappear when fields are separated
# in unquoted arguments.
printf 'space between:X%sX\n' $b $m $a
printf 'no space between:X%sX\n' $b$m$a

which produces the output:
Code:
"v1":Xbefore echo d   a b afterX
v1:XbeforeX
v1:XechoX
v1:XdX
v1:XaX
v1:XbX
v1:XafterX
d   a b after
"v2":XX
v2:XX
v1string:XbeforeX
v1string:XechoX
v1string:XdX
v1string:X X
v1string:XaX
v1string:XbX
v1string:XafterX
v2string:XbeforeX
v2string:XechoX
v2string:XdX
v2string:X X
v2string:XaX
v2string:XbX
v2string:XafterX
w=$b $m $a
+ w='before '
+ middle after
tester: line 28: middle: command not found
z=$b$m$a
+ z='before  middle  after'
set +xv
+ set +xv
space between:XbeforeX
space between:XmiddleX
space between:XafterX
no space between:XbeforeX
no space between:XmiddleX
no space between:XafterX

So, in your statement:
Code:
txts="\x1B[0m"$y" "$x"This is bizarre, why the inverted commas error?"

it does what you want even though the expansions $y and $x are not quoted (because it is an assignment statement). But, why are you adding the extra double-quotes? They don't do anything but make the code harder to read. Variable expansions, command substitutions, and arithmetic expansion are all processed in double-quoted strings, so why not simplify your code and make it easier to read and understand:
Code:
txts="\x1B[0m$y ${x}This is bizarre, why the inverted commas error?"

(which is a single double-quoted string). Or, if you are really against using braces to keep variable names separate from following text:
Code:
txts='\x1B[0m'"$y $x"'This is bizarre, why the inverted commas error?'

(which has the constant characters in the two single-quoted strings and the variable expansions in a single double-quoted string).
And, besides being easier to read and understand, I would think either of these should make shellcheck happy too. (But I don't have shellcheck installed on my system; so I haven't verified that.)

I should also have mentioned in earlier posts on this subject that \x followed by two hexadecimal digits is a common extension provided by many implementations of the printf utility (both stand-alone and as shell built-ins), but it is not mentioned in the standards. Similarly, \e is a common extension that expands to an escape character in the current locale provided by many implementations of the printf utility, but it is not mentioned in the standards either. With any standards conforming version of printf, you can use the octal escape sequence \033 to specify an escape character in locales with an underlying codeset that is a superset of ASCII.
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I get the unused space?

One of my Solaris 8 machines hd was about to die. So I used g4u to create an image of the 9gb drive and I put it in a 36gb drive. That solved my dieing hd problem. But.... How do I get my machine to see the unused 27gb of space? Any help would be greatly appreciated. (1 Reply)
Discussion started by: Spyzic
1 Replies

2. AIX

unused storage on AIX 4.3

Hi, How do I query for unused partition in AIX 4.3 with DAS and SAA storage? I know most unix administrator don't put all the capacity on the system at once. thanks, vene (1 Reply)
Discussion started by: venerayan
1 Replies

3. UNIX for Dummies Questions & Answers

delete the unused file

Hi All, Can you please let me know how to delete any files that have not been accessed in the past 28 days in a directory. Thanks, Arun (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

4. AIX

how to clean Unused semaphore??

How can i clean up my unused semaphore??? (4 Replies)
Discussion started by: abhishek27
4 Replies

5. HP-UX

HP-UX using unused HDD space

Hello, I have a system with HP-UX 11.23 installed on it. There are ~36GB of unused space on the HDD. I did a very basic installation, and it created the usual volume group /dev/vg00. When I look at the output of ioscan -funC disk, I see this (and more, but irrelevant to this post): disk ... (1 Reply)
Discussion started by: goon12
1 Replies

6. Solaris

unused disk space

i Have alloted 20G in my vmware for solaris 10, upon installation, and some distribution of disk space to /,/opt,swap i just use 19G. Can i still use the 1G? How? how to see the 1G? that i did not use? how can i use it? appreciate your responce (17 Replies)
Discussion started by: kenshinhimura
17 Replies

7. AIX

Temporarily disabling unused ethernet adapter

Hi, In our AIX 5.2 server , we have one unused ethernet adapter which doesn't have cable connection . For this interface , we are getting alerts in errpt . Could you suggesthow to stop this alert ? And sametime i would like to keep this device in ODM . Is there... (1 Reply)
Discussion started by: sekarsamy
1 Replies

8. Programming

What Unix do with unused shared memory?

Hello, When creating shared memory in C, should be remove shared memory with shmctl function when don't need it. If it didn't remove, occupied shared memory stay and remain. If we create shared memory repeatedly without removing unusable shared memory, /dev/shm will full. Does Unix or... (1 Reply)
Discussion started by: pronetin
1 Replies

9. Solaris

Solaris 10: how to disable an unused HBA card

Dear all, I have a new Oracle Blade X4-2B server, running Solaris 10. The server comes with a HBA card that will not be used now. It has not fibers connected to it. As a consequence, its leds never stop flashing. My question is: how to disable this HBA card, without removing it physically... (2 Replies)
Discussion started by: Gus1971
2 Replies
All times are GMT -4. The time now is 02:53 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy