Uppercase variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Uppercase variable
# 1  
Old 04-14-2019
Uppercase variable

Hello,
This is not a problem specific question.
While surfing on google to find a solution for the latest error messages I have received from command line, I found some suggestions regarding usage of linux commands:

Quote:
Use $( ), and don't use `` anymore: Bash FAQ 82.
Could it be a problem specific comment or in most cases, `` causes issue?
Normally I have been using this though and did not encounter any issue.

Quote:
Avoid using UPPERCASE variable names. That namespace is generally reserved by the shell for special purposes (like PATH), so using it for your own variables is a bad idea.
How come would linux detect uppercase variables as path?

Thank you
Boris
# 2  
Old 04-14-2019
Quote:
Could it be a problem specific comment or in most cases, `` causes issue?
Normally I have been using this though and did not encounter any issue.
No.
The problem relates to nested ``, a pair of back-ticks inside another pair of back-ticks will cause errors.

Quote:
How come would Linux detect uppercase variables as path?
It has nothing to do with PATH particularly. What if you made a variable called "BASH_REMATCH", what would happen? I picked this one because it does not come up very often here on the forums.
Answer: It is the name of an array. Your code could break regular expression searches in bash. None of the special variables bash uses are lowercase.
So, no matter what you do to lowercase variables, you will not mess up shell and other utilities do internally.

This is the output of the set command on my cygwin instance on Windows 7: It's BIG. Many,many variables. If
I use other than default software the variables may increase a lot - Oracle is an example.
Code:
$ set
ALLUSERSPROFILE='C:\ProgramData'
APPDATA='C:\Users\Owner\AppData\Roaming'
BASH=/bin/bash
BASHOPTS=cmdhist:complete_fullquote:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="4" [2]="12" [3]="3" [4]="release" [5]="x86_64-unknown-cygwin")
BASH_VERSION='4.4.12(3)-release'
COMMONPROGRAMFILES='C:\Program Files\Common Files'
COMPUTERNAME=OWNER-PC
COMSPEC='C:\Windows\system32\cmd.exe'
CYG_SYS_BASHRC=1
CommonProgramW6432='C:\Program Files\Common Files'
DIRSTACK=()
EUID=1000
EXECIGNORE='*.dll'
FP_NO_HOST_CHECK=NO
GROUPS=()
HISTFILE=/home/Owner/.bash_history
HISTFILESIZE=500
HISTSIZE=500
HOME=/home/Owner
HOMEDRIVE=C:
HOMEPATH='\Users\Owner'
HOSTNAME=Owner-PC
HOSTTYPE=x86_64
IFS=$' \t\n'
INFOPATH=/usr/local/info:/usr/share/info:/usr/info
LANG=en_US.UTF-8
LOCALAPPDATA='C:\Users\Owner\AppData\Local'
LOGONSERVER='\\OWNER-PC'
MACHTYPE=x86_64-unknown-cygwin
MAILCHECK=60
MINTTY_SHORTCUT='/cygdrive/c/Users/Public/Desktop/Cygwin64 Terminal.lnk'
NUMBER_OF_PROCESSORS=8
OLDPWD=/cygdrive/c/Users/Owner/Desktop
OPTERR=1
OPTIND=1
ORIGINAL_PATH='/cygdrive/c/ProgramData/Oracle/Java/javapath:/cygdrive/c/Program Files (x86)/Intel/iCLS Client:/cygdrive/c/Program Files/Intel/iCLS Client:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/c/Program Files/Intel/Intel(R) Management Engine Components/DAL:/cygdrive/c/Program Files/Intel/Intel(R) Management Engine Components/IPT:/cygdrive/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL:/cygdrive/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/IPT:/cygdrive/c/Program Files (x86)/Intel/OpenCL SDK/2.0/bin/x86:/cygdrive/c/Program Files (x86)/Intel/OpenCL SDK/2.0/bin/x64:/cygdrive/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/c/Program Files (x86)/Skype/Phone:/cygdrive/c/Users/Owner/.cargo/bin:/cygdrive/c/Users/Owner/AppData/Local/Programs/Python/Python35/Scripts:/cygdrive/c/Users/Owner/AppData/Local/Programs/Python/Python35:/cygdrive/c/Program Files (x86)/IDM Computer Solutions/UltraEdit'
OS=Windows_NT
OSTYPE=cygwin
PATH='/usr/local/bin:/usr/bin:/cygdrive/c/ProgramData/Oracle/Java/javapath:/cygdrive/c/Program Files (x86)/Intel/iCLS Client:/cygdrive/c/Program Files/Intel/iCLS Client:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/c/Program Files/Intel/Intel(R) Management Engine Components/DAL:/cygdrive/c/Program Files/Intel/Intel(R) Management Engine Components/IPT:/cygdrive/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL:/cygdrive/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/IPT:/cygdrive/c/Program Files (x86)/Intel/OpenCL SDK/2.0/bin/x86:/cygdrive/c/Program Files (x86)/Intel/OpenCL SDK/2.0/bin/x64:/cygdrive/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/cygdrive/c/Windows/system32:/cygdrive/c/Windows:/cygdrive/c/Windows/System32/Wbem:/cygdrive/c/Windows/System32/WindowsPowerShell/v1.0:/cygdrive/c/Program Files (x86)/Skype/Phone:/cygdrive/c/Users/Owner/.cargo/bin:/cygdrive/c/Users/Owner/AppData/Local/Programs/Python/Python35/Scripts:/cygdrive/c/Users/Owner/AppData/Local/Programs/Python/Python35:/cygdrive/c/Program Files (x86)/IDM Computer Solutions/UltraEdit:/usr/lib/lapack'
PATHEXT='.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC'
PIPESTATUS=([0]="0")
PPID=35
PRINTER='HP Color LaserJet Pro MFP M477 PCL 6'
PROCESSOR_ARCHITECTURE=AMD64
PROCESSOR_IDENTIFIER='Intel64 Family 6 Model 58 Stepping 9, GenuineIntel'
PROCESSOR_LEVEL=6
PROCESSOR_REVISION=3a09
PROFILEREAD=true
PROGRAMFILES='C:\Program Files'
PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h \[\e[33m\]\w\[\e[0m\]\n\$ '
PS2='> '
PS4='+ '
PSModulePath='C:\Windows\system32\WindowsPowerShell\v1.0\Modules\'
PUBLIC='C:\Users\Public'
PWD=/home/Owner
ProgramData='C:\ProgramData'
ProgramW6432='C:\Program Files'
SESSIONNAME=Console
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=1
SYSTEMDRIVE=C:
SYSTEMROOT='C:\Windows'
TEMP=/tmp
TERM=xterm
TMP=/tmp
TZ=America/Denver
UID=1000
USER=Owner
USERDOMAIN=Owner-PC
USERNAME=Owner
USERPROFILE='C:\Users\Owner'
WINDIR='C:\Windows'
_=env
windows_tracing_flags=3
windows_tracing_logfile='C:\BVTBin\Tests\installpackage\csilogfile.log'
gawklibpath_append ()
{
    [ -z "$AWKLIBPATH" ] && AWKLIBPATH=`gawk 'BEGIN {print ENVIRON["AWKLIBPATH"]}'`;
    export AWKLIBPATH="$AWKLIBPATH:$*"
}
gawklibpath_default ()
{
    unset AWKLIBPATH;
    export AWKLIBPATH=`gawk 'BEGIN {print ENVIRON["AWKLIBPATH"]}'`
}
gawklibpath_prepend ()
{
    [ -z "$AWKLIBPATH" ] && AWKLIBPATH=`gawk 'BEGIN {print ENVIRON["AWKLIBPATH"]}'`;
    export AWKLIBPATH="$*:$AWKLIBPATH"
}
gawkpath_append ()
{
    [ -z "$AWKPATH" ] && AWKPATH=`gawk 'BEGIN {print ENVIRON["AWKPATH"]}'`;
    export AWKPATH="$AWKPATH:$*"
}
gawkpath_default ()
{
    unset AWKPATH;
    export AWKPATH=`gawk 'BEGIN {print ENVIRON["AWKPATH"]}'`
}
gawkpath_prepend ()
{
    [ -z "$AWKPATH" ] && AWKPATH=`gawk 'BEGIN {print ENVIRON["AWKPATH"]}'`;
    export AWKPATH="$*:$AWKPATH"
}
profile_d ()
{
    _LC_ALL_SET_="${LC_ALL+set}";
    _LC_SAVE_="${LC_ALL-null}";
    LC_ALL=C;
    if [ "${_LC_SAVE_}" = "null" ]; then
        for file in /etc/profile.d/*.$1;
        do
            [ -e "${file}" ] && . "${file}";
        done;
        unset LC_ALL;
    else
        for file in /etc/profile.d/*.$1;
        do
            [ -e "${file}" ] && LC_ALL="${_LC_SAVE_}" . "${file}";
        done;
        LC_ALL="${_LC_SAVE_}";
    fi;
    unset file;
    unset _LC_ALL_SET_;
    unset _LC_SAVE_
}

Owner@Owner-PC ~
$


Last edited by jim mcnamara; 04-14-2019 at 09:46 PM..
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 04-15-2019
Well Jim, it's nice to see that there are historical backticks in the output of set for things such as gawkpath_default Smilie

I must admit that I spent a few years writing awful code using backticks in my scripts and fighting with escapes and escaped escapes depending how many layers there were or if I was running things remotely. Compare these:-
Code:
ssh $trg grep  "'warning'  \`ls /app/logs/*\\\`date '+%Y'\\\`.TFC-log|sort -r\`"
ssh $trg grep  "'warning'  \$(ls /app/logs/*\$(date '+%Y').TFC-log|sort -r)"

Both are pretty awful and should probably have been replaced by something neater overall anyway, but the escaped back-ticks make it particularly horrible to decipher. Sadly I had to escape the $ to get that passed as literal test to the remote server so that it would then execute there. I'm not proud of it but it worked when we hardly knew what we were doing and so this sort of thing got used all over the place for a while, so deciphering it (and worse) became quite an effort. It is all tidied up/replaced now anyway. They are worse to understand if you are not the author because the scripts were rarely commented.

The really confusing part is trying to work out where a back-tick ends with all those escapes too. At least with the start of a call being $( and the end being ) you have a fighting chance and tools like vim can help you too because they recognise and highlight the 'other end' and a call.



I hope that this help (or my awful old-style code makes you squirm Smilie)
Robin
These 4 Users Gave Thanks to rbatte1 For This Post:
# 4  
Old 04-15-2019
Hi all...

Bizarrely, I have switched from lowercase to uppercase for variables as they are easier to see straight away. The vast majority of tools/commands/utilities are lowercase and can _mingle_ with the variables visually whereas uppercase tend to stand out. I do pick names that are very literal and unlikely to coincide with those of the/any environment.

Examples: VERTICAL_GAIN and AUDIO_SAMPLE ...

On AudioScope I was _briefed_ about using back-ticks but they have long since gone, along with this, which is still valid on OSX 10.14.3's bash version, 3.2.57:
x=1; x=$[ ( x + 4 ) ]; echo "$x"<CR> ...
This User Gave Thanks to wisecracker For This Post:
# 5  
Old 04-15-2019
Possible exception quibble

Hi, Jim.

Quote:
Originally Posted by jim mcnamara
So, no matter what you do to lowercase variables, you will not mess up shell and other utilities do internally.
With the possible exception of csh, tcsh and friends, which use variable names like path and then they will make changes to the environment variable like PATH automatically (as I recall).

But in terms of Bourne shell and descendants, I agree, and good point ... cheers, drl
# 6  
Old 04-15-2019
Regarding
Code:
ssh $trg grep  "'warning'  \$(ls /app/logs/*\$(date '+%Y').TFC-log|sort -r)"

You do not want to substitute any $-expression on the remote site by the calling site.
Then it is easier to use protective 'ticks' for the local site, so the remote side gets its contents unchanged. And have \' or " inside it that becomes ' or " on the remote side.
Code:
ssh $trg 'grep  "warning" $(ls /app/logs/*$(date "+%Y").TFC-log|sort -r)'

Or, assuming that ls -r does the same as sort -r,
Code:
ssh $trg 'grep  "warning" $(ls -r /app/logs/*$(date "+%Y").TFC-log)'

# 7  
Old 04-16-2019
Yes, I said that even the better version was awful code Smilie and I'm having to remember it because it's long since replaced/retired. I might even have a syntax error, but I just remember the horror of all those escapes Smilie


I'm off for a lie down.
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Uppercase to lowercase

Hello, I have a list of files in a directory whose names are all in uppercasse, including the file format for eg *.MP3 . I would like to convert these to the normal way we write it ie ABC.MP3 to be converted to Abc.mp3 . I know that this can be done manually by using a lot of "mv" or rename... (6 Replies)
Discussion started by: ajayram
6 Replies

2. Shell Programming and Scripting

Change to uppercase

Hi I have a string(can be mix of upper and lower case) and need the first three chars of the string to be converted to uppercase (4 Replies)
Discussion started by: Northpole
4 Replies

3. UNIX for Dummies Questions & Answers

UPPERCASE to lowercase

Hi All, i have a file and i want to convert all uppercase letters to lowercase letters which are in my file. how can i do this. Thanx (3 Replies)
Discussion started by: temhem
3 Replies

4. UNIX for Dummies Questions & Answers

uppercase to lowercase

i have no variable and no file i just want to convert AJIT to ajit with some command in UNIX can anybody help (4 Replies)
Discussion started by: ajit.yadav83
4 Replies

5. AIX

Lowercase to Uppercase

Inside a script I have 2 variables COMP=cy and PT=t. further down the same script I require at the same line to call those 2 variables the first time uppercase and after lowercase ${COMP}${PT}ACE,${COMP}${PT}ace. Can somebody help me Thanks in advance George Govotsis (7 Replies)
Discussion started by: ggovotsis
7 Replies

6. Shell Programming and Scripting

only uppercase first character?

should be a simple question, I am trying to uppercase every first character in a word on the list. abc google cnn services My first thought was sed 'y/^/^/', but it changed all the characters, not just the first character. any thoughts? (7 Replies)
Discussion started by: fedora
7 Replies

7. Shell Programming and Scripting

Conversion to uppercase - tr

Hi, I am trying to convert the $i in loop from lower to upper case but getting error like ' awk: 0602-502 The statement cannot be correctly parsed. The source line is 1.' .. Requirement: I have many table in script XXX.sql which starting with 'ABC_','AbC_','aBc_' etc.. same thing for table... (2 Replies)
Discussion started by: subrat
2 Replies

8. UNIX for Dummies Questions & Answers

make uppercase

If in a script I am taking an input (R201) for example and assigning it to a variable, how would I change the R to uppercase if it was keyed in as r201? I can't seem to get it to work with toupper (4 Replies)
Discussion started by: kirkm76
4 Replies

9. Shell Programming and Scripting

Converting to Uppercase

I want to convert string into uppercase string. How can i do that ? Ex: Enter the user name: read name show=upper(name) echo $show --- This output should be the uppercase output. Thanks (3 Replies)
Discussion started by: dreams5617
3 Replies

10. Shell Programming and Scripting

uppercase to lowercase

Greetings & Happy New Years To All! A client of mine FTP'ed their files up to the server and it all ended up being in UPPERCASE when it all should be in lowercase. Is there a builtin command or a script anyone knows of that will automagically convert all files to lowercase? Please advise asap... (4 Replies)
Discussion started by: webex
4 Replies
Login or Register to Ask a Question