0

I'm trying to understand the command below, and particularly the parts that are in bold.

I understand what the second line is. It's testing if a file exists and if it doesn't, it creates one, but what does $_1/ mean?

test "$1" = "0" && exit test -f $_1/samplefile || touch $_1/samplefile 
2
  • Depends if this is just a snippet. I saw a convention recently that _1, _2 etc were assigned with args to a function (or a whole script -- cant now recall). So the answer may lie somewhere else in the script, or as one-off exports on the command line that invoked it. Commented Jul 7, 2020 at 17:32
  • Sorry: to clarify: saw the construct: _1=alpha _2=beta myCommand with the intent that args to myCommand are in local environment, ordered but not positional, and are also global even within functions. Commented Jul 8, 2020 at 9:34

2 Answers 2

3

$_1 means "expand the variable _1". Since this variable was not set, it will expand to nothing. Someone probably made a mistake.

It may be that the person thought $_1 would be the same as ${_}1, i.e., the expansion of $_, (which is the last argument to the previous command), concatenated with the number 1. But it is not the same, as can be seen below:

$ cat tst.bash #!/bin/bash test "$1" = "0" && exit echo $_ test "$1" = "0" && exit echo $_1 test "$1" = "0" && exit echo ${_}1 
$ ./tst.bash 0 01 

If you provide anything but the number 0 as an argument to the script (in the sample above, I provided no argument at all), then the test fails and exit is not triggered. So, for each echo, the last command executed is

test "$1" = "0" 

, whose last argument is 0. Thus, $_ expands to 0, ${_}1 expands to 01 and $_1 expands to the empty string.

1
  • $1 would be more sensible as part of the path since $_ would always be 0 in the second line, like your sample script shows. It doesn't seem like a simple typo, though, since it appears twice and I don't think _ is close to 1 or $ on the common keyboard layouts.. Commented Jul 7, 2020 at 14:59
0

Thank you guys. I think that the person who wrote probably made a mistake. The propose was to check if there is a file and if not create a file. So, after some tries, I used test -f $(pwd)”/samplefile” || touch samplefile and it works. I just got confused because all commands are new for me, and I thought that maybe $_1/ could mean something. Thank you so much! :-)

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.