Zenity --text with string variable - linux

I'm wondering how I can pass a string containing spaces to Zenity for the text argument as my current method is truncating/failed to evaluate all the text after the first space.
Here is an MVP which shows the issue:
Script
#!/bin/bash
a="test test test"
test_func() {
echo "$#"
$(zenity --info --text "test test")
$(zenity --info --text "$#")
}
test_func ${a}
Output
$> test test test
$> (zenity info window with test test as text)
$> (zenity info window with test) *** should contain "test test test"

Use "$*" instead of "$#".
Manual for "$#" (emphasis mine):
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$#" is equivalent to "$1" "$2" ... If the double-quoted expan sion occurs within a word, the expansion of the first parameter is joined with the beginning part of the orig‐ inal word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$#" and $# expand to nothing (i.e., they are removed).
And for "$*":
Expands to the positional parameters, starting from one. When the expansion is not within double quotes, each positional parameter expands to a separate word. In contexts where it is performed, those words are subject to further word splitting and pathname expansion. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
Alternatively, you could quote ${a} to pass a as a single argument instead of splitting on spaces.

Related

Linux Bash's rule for determing a variable name Greedy or Non-Greedy

If you run the following bash script on Ubuntu 12.04:
t="t"
echo "1st Output this $15"
echo "2nd this $test"
The output is:
1st Output this 5
2nd this
How can the first echo takes $1 as a variable name (non-greedy) interpreting it as ${1}5 while the second echo takes $test as a variable name (greedy) instead of interpreting it as ${t}est?
There are two parts to your question:
$15 would always be interpreted as $1, i.e. the first positional parameter1, concatenated with 5. In order to use the fifteenth positional parameter, you'd need to say ${15}.
$test would be interpreted as variable test. So if you want it to be interpreted as $t concatenated with est, then you need to say ${t}est
1Quoting from info bash:
When a positional parameter consisting of more than a single digit is
expanded, it must be enclosed in braces (see EXPANSION below).
...
EXPANSION
Expansion is performed on the command line after it has been split into
words. There are seven kinds of expansion performed: brace expansion,
tilde expansion, parameter and variable expansion, command substitu‐
tion, arithmetic expansion, word splitting, and pathname expansion.
You may also want to refer to:
What's the difference between ${varname} and $varname in a shell scripts

decrypting a variable in a scripting environment of Linux

What does $# in unix shell script signify. For example:
A__JOB="$CLASS $#"
where $CLASS has my java class file name. So what might be the meaning of
$#.
What did I do?
I Googled :) but $# seems to be complex query for ir or maybe i do not know how to search google for special characters.
$# is the value of all arguments passed.
For example, if you pass:
./script A B C D
then "$#" will be equal to "A" "B" "C" "D"
So it looks like the purpose is to passe all the arguments passed to the script directly to the java program.
From bash manual:
# Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, each parameter
expands to a separate word. That is, "$#" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs
within a
word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the
last parameter is joined with the last part of the original word. When there are no positional parameters, "$#" and $# expand
to nothing (i.e., they are removed).

How to echo "$x_$y" in Bash script?

It is very interesting that if you intend to display 0_1 with Bash using the code
x=0
y=1
echo "$x_$y"
then it will only display
1
I tried echo "$x\_$y" and it doesn't work.
How can I echo the form $x_$y? I'm going to use it on a file name string.
Because variable names are allowed to have underscores in them, the command:
echo "$x_$y"
is trying to echo ${x_} (which is probably empty in your case) followed by ${y}. The reason for this is because parameter expansion is a greedy operation - it will take as many legal characters as possible after the $ to form a variable name.
The relevant part of the bash manpage states:
The $ character introduces parameter expansion, command substitution, or arithmetic expansion.
The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name.
When braces are used, the matching ending brace is the first } not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion.
Hence, the solution is to ensure that the _ is not treated as part of the first variable, which can be done with:
echo "${x}_${y}"
I tend to do all my bash variables like this, even standalone ones like:
echo "${x}"
since it's more explicit, and I've been bitten so many times in the past :-)
This way:
$ echo "${x}_${y}"
0_1
wrap it in curly braces:
echo "${x}_${y}"
Just to buck the trend, you can also do this:
echo $x'_'$y
You can have quoted and unquoted parts next to each other with no space between. And since ' isn't a legal character for a variable name, bash will substitute only $x. :)

Whitespace as an argument of a ksh script

Suppose you have the following script:
#!/usr/bin/ksh
for element in $#
do
echo $element
done
And you execute the script using:
./myscript.ksh "my element"
The expected output should be:
my element
but the whitespace is treated as a separator for each argument, obtaining:
my
element
How should I escape the whitespace?
Thanks
You must enclose $#, see Special Parameters, in double quotes:
($#) Expands to the positional parameters, starting from one.
...
When the expansion occurs within double quotes, and word splitting
is performed, each parameter expands to a separate word. That is,
"$#" is equivalent to "$1" "$2" ….
#!/usr/bin/ksh
for element in "$#"
do
echo $element
done
You can get the same output by omitting in "$#" (Looping Constructs, for) altogether:
If ‘in words’ is not present, the for command executes the commands
once for each positional parameter that is set, as if ‘in "$#"’ had
been specified
#!/usr/bin/ksh
for element
do
echo $element
done
You must either quote the $#, or use nothing:
for element in "$#"
or
for element
will both work.

What does $# mean in a shell script?

What does a dollar sign followed by an at-sign (#) mean in a shell script?
For example:
umbrella_corp_options $#
$# is all of the parameters passed to the script.
For instance, if you call ./someScript.sh foo bar then $# will be equal to foo bar.
If you do:
./someScript.sh foo bar
and then inside someScript.sh reference:
umbrella_corp_options "$#"
this will be passed to umbrella_corp_options with each individual parameter enclosed in double quotes, allowing to take parameters with blank space from the caller and pass them on.
$# is nearly the same as $*, both meaning "all command line arguments". They are often used to simply pass all arguments to another program (thus forming a wrapper around that other program).
The difference between the two syntaxes shows up when you have an argument with spaces in it (e.g.) and put $# in double quotes:
wrappedProgram "$#"
# ^^^ this is correct and will hand over all arguments in the way
# we received them, i. e. as several arguments, each of them
# containing all the spaces and other uglinesses they have.
wrappedProgram "$*"
# ^^^ this will hand over exactly one argument, containing all
# original arguments, separated by single spaces.
wrappedProgram $*
# ^^^ this will join all arguments by single spaces as well and
# will then split the string as the shell does on the command
# line, thus it will split an argument containing spaces into
# several arguments.
Example: Calling
wrapper "one two three" four five "six seven"
will result in:
"$#": wrappedProgram "one two three" four five "six seven"
"$*": wrappedProgram "one two three four five six seven"
^^^^ These spaces are part of the first
argument and are not changed.
$*: wrappedProgram one two three four five six seven
These are the command line arguments where:
$# = stores all the arguments in a list of string
$* = stores all the arguments as a single string
$# = stores the number of arguments
The usage of a pure $# means in most cases "hurt the programmer as hard as you can", because in most cases it leads to problems with word separation and with spaces and other characters in arguments.
In (guessed) 99% of all cases, it is required to enclose it in ": "$#" is what can be used to reliably iterate over the arguments.
for a in "$#"; do something_with "$a"; done
Meaning.
In brief, $# expands to the arguments passed from the caller to a function or a script. Its meaning is context-dependent: Inside a function, it expands to the arguments passed to such function. If used in a script (outside a function), it expands to the arguments passed to such script.
$ cat my-script
#! /bin/sh
echo "$#"
$ ./my-script "Hi!"
Hi!
$ put () { echo "$#"; }
$ put "Hi!"
Hi!
* Note: Word splitting.
The shell splits tokens based on the contents of the IFS environment variable. Its default value is \t\n; i.e., whitespace, tab, and newline. Expanding "$#" gives you a pristine copy of the arguments passed. Expanding $# may not. More specifically, any arguments containing characters present in IFS might split into two or more arguments or get truncated.
Thus, most of the time what you will want to use is "$#", not $#.
From the manual:
#
Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$#" is equivalent to "$1" "$2" .... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$#" and $# expand to nothing (i.e., they are removed).
$# is basically use for refers all the command-line arguments of shell-script.
$1 , $2 , $3 refer to the first command-line argument, the second command-line argument, third argument.
They are often used to simply pass all arguments to another program
[root#node1 shell]# ./my-script hi 11 33
hi 11 33
[root#node1

Resources