2

I needed help understanding what these couple of lines mean. I need to make some changes to a bash script for my internship, and since I just started reading up on bash today, I'm having a hard time understanding what this would mean.

node="\\\\$1"

echo $node`echo $2|sed 's/\//\\\\/g'`

Thanks in advance

3 Answers 3

4

This line prepends two backslashes to the first argument to the script. The first backslash in each pair escapes the second.

node="\\\\$1"

This line takes the result of the previous one and appends the result of replacing each of the slashes in the second argument with two backslashes.

echo $node`echo $2|sed 's/\//\\\\/g'`

If you were to call your script like this:

scriptname abc /def/ghi/jkl

then $node would be "\\abc" and the following would be echoed:

\\abc\\def\\ghi\\jkl

The $1 and $2 are referred to as positional parameters and represent the first and second arguments to your script. If you wanted to refer to all of them together regardless of how many (up to the limit that your system permits), you would use $@ or $*.

Here is a better way to write those two lines:

node='\\'"$1"

By using single quotes around the backslashes, you don't need to escape them. Using double quotes around the positional parameter allows it to be expanded.

echo $node$(echo $2 | sed 's|/|\\\\|g')

Using $() instead of backticks is more readable, they can easily be nested and quoting and escaping are simplified. By using an alternate delimiter (in this case the pipe symbol) with sed you don't need to escape the slash and the whole thing is more readable. The backslashes still need to be escaped in this case since sed is processing them rather than Bash.

If you're not familiar with $() (or backticks) they perform command substitution which means that the output of the enclosed command(s) is substituted in their place.

You may not be familiar with sed. It is a utility that is external to Bash (or other shells) which can take a Stream of characters and EDit them. The particular command in use here is s which stands for substitute. It will substitute what's found between the second and third delimiters (usually slashes, but you can use almost any character) for the pattern between the first and second delimiters. The g is a modifier that causes the substitution to be made globally (i.e. each time it occurs in the input line). The process is repeated for each line of input. In your example there's only one line of input - the output of the echo.

0
1

1st line: node is a variable set from the command line. So, if you do

runme.sh test

node will equal "\test". The first slash escapes the second slash, treating it as a literal slash instead of a special modifier.

Then, it replaces any backslashes in the name with two forward slashes, like so

> runme.sh test/ing

\\test\\ing

Then, that is likely interpreted by another program, which interpenetrates the slashes, thus requiring there to be two of them.

1
  • You have your terminology reversed: / is a "forward" slash. `\` is a backslash. Sep 5, 2010 at 5:46
1

It's not clear which parts of those lines you are having trouble understanding, so I'll try explaining what I think are the sticky parts.

The script you are working on takes at least two arguments. These arguments are accessed within the script by the names $1 and $2. In the first line, the first backslash escapes the character following it, the second backslash, so that the pair is evaluated as a single literal backslash. The same is true for the second pair of backslashes. The $1 is replaced by the value of the first argument to the script. If the script is run as

myscript apple banana/cantaloupe

the value of node will be "\\apple", without the quotes.

Commands within back ticks (`) are executed, then the back ticks and the enclosed commands are replaced by the output of those commands. The sed command replaces each forward slash in its input stream by a pair of backslashes. Since $2 in this example is "banana/cantaloupe", the result of the commands within the back ticks would be "banana\\cantaloupe". The output of the second line is the value of node concatenated with the result of the back-tick expression, or "\\applebanana\\cantaloupe".

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .