ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
as you see i had to use twice the same command, any way of avoiding this?
Imagine i want to assign each of the data columns in the result to a new variable instead of the last two...
p.s.1. This data is fictitious, at least the echo part inside the "$()"
# Here string
read x x x foo bar x <<< $data
echo "foo is '$foo' and bar is '$bar'"
# Here document
foo= bar=
read x x x foo bar x <<- EoF
$data
EoF
echo "foo is '$foo' and bar is '$bar'"
# Array
foo= bar=
array=( $data )
foo=${array[3]}
bar=${array[4]}
echo "foo is '$foo' and bar is '$bar'"
Well now that just sounds painful .... just jokes .. very nice, although for the array I would just refer directly to it rather than passing on to other variables.
And in plain english, for dummies you did?
care of explaining that piece of code please
Quote:
Originally Posted by catkin
Cat skinning
Code:
# Here string
read x x x foo bar x <<< $data
echo "foo is '$foo' and bar is '$bar'"
# Here document
foo= bar=
read x x x foo bar x <<- EoF
$data
EoF
echo "foo is '$foo' and bar is '$bar'"
# Array
foo= bar=
array=( $data )
foo=${array[3]}
bar=${array[4]}
echo "foo is '$foo' and bar is '$bar'"
Instead of the dummy 'x', you can actually just use '_'
Is there something special about "_"? I vaguely recall the ksh read command allowed "." as a placeholder and was wanting for something similar in bash but _ is a valid (if unusual) variable name. Or is it? This command prompt experiment did not work as expected:
Code:
c@CW8:~$ read _ rest
foo bar
c@CW8:~$ echo $_
rest
c@CW8:~$ echo $rest
bar
c@CW8:~$ read a b
foo bar
c@CW8:~$ echo $a
foo
c@CW8:~$ echo $b
bar
At shell startup, set to the absolute pathname used to invoke
the shell or shell script being executed as passed in the envi‐
ronment or argument list. Subsequently, expands to the last
argument to the previous command, after expansion. Also set to
the full pathname used to invoke each command executed and
placed in the environment exported to that command. When check‐
ing mail, this parameter holds the name of the mail file cur‐
rently being checked.
The _ in a read, though, is an explicit placeholder that tells bash to throw that value away, not a variable.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.