1

I am using a KSH script that creates numerous variables from fairly complex code. What command can I place in the script to view the value of these variables as they are created? Would ECHO be the correct command, in a line like echo variable is $variable_name?

  • Are you looking for echo "$variable"? – Joseph R. Nov 4 '13 at 20:16
  • try export or set – michas Nov 4 '13 at 20:32
  • 1
    You could add set -x before the code that creates the vars and set +x after. Might generate too much output for you. – glenn jackman Nov 4 '13 at 22:11
1

In all likelyhood, what you want is set -x. This activates trace mode: the shell prints each command before running it. Put this as the second line of your script, just below the #! line. If you want to turn off traces in a part of the script, use set +x.

To display the value of a variable, you can use echo "$somevariable", but this is not always be good enough. Be sure not to omit the double quotes: without double quotes, the value of the variable is interpreted as a list of file name wildcard patterns instead of a string. There are a few values of the variable for which echo doesn't work. Under ksh, I believe the exhaustive list is -e, -n, -ne and -en, all of which are interpreted as options to echo. To avoid this interpretation, use print or printf instead (the two commands below are equivalent):

print -r -- "$somevariable"
printf '%s\n' "$somevariable"

If the value of the variable contains trailing whitespace or nonprinting characters, this won't show on screen. Use the %q escape to printf to instead print a nonambiguous representation of the value of the variable.

printf '$%s is %q\n' somevariable "$somevariable"

You can put this in a function.

trace_variables () {
  typeset _trace_variables__var
  for _trace_variables__var; do
    typeset -n _trace_variables__ref="$_trace_variables__var"
    printf "%s = %q\\n" "$_trace_variables__var" "$_trace_variables__ref"
  done
}
…
trace_variables somevariable anothervariable
-1

In ksh, proper use of variable substitutions is ${variable_name}. And yes, you can echo this to your terminal or use it in a test command, i.e. if [ ${variable_name} - eq ${mynumber} ] etc.

If you skip using the curly brackets, it still works most of the time but, you will be more prone to making mistakes. So my suggestion is to use them at all times.

  • Not great advice: don't mistake braces for double quotes. Your if condition will fail if $variable_name contains space-separated words. The braces are not required if you're doing a simple parameter expansion. See the Parameter Expansion section of the manual for all the situations where the braces are required. – glenn jackman Nov 4 '13 at 22:05
  • The braces are rarely needed and there is nothing wrong with omitting them the 99.9% of the time they aren't needed. On the other hand, double quotes are needed more often than not. See $VAR vs ${VAR} and to quote or not to quote. Besides the misleading advice, you aren't really answering the question. – Gilles Nov 4 '13 at 22:06
  • if you don't use braces, you are letting the shell take over your variable expansion with the characters on the command line following the variable nae without a known delimiter. IT IS ALWAYS SAFER TO USE CURLY BRACKETS. Usage of double quotes on the other hand, is up to the user, knowing the nature of values, his or her variables can take. If you are dealing with numeric variables and values of the numerals, in the same context you downed my idea, what is the use of double quotes ? If you are doing string evaluation, yes they are of some use. – MelBurslan Nov 4 '13 at 22:40
  • If you're concerned about mistakes, use curly braces and test for validity with ${varname:?}. This, or the global set -u will trigger an error on unset variables. It catches typos in the variable names. – Henk Langeveld Nov 5 '13 at 20:00
  • 2
    @Mel_Burslan This is completely backwards. It's immediately apparent in the script whether the braces are needed: it only depends on the syntax of the script. On the contrary, it's never apparent whether double quotes are needed: it depends on the value of the variable. – Gilles Nov 6 '13 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.