Bash quirk when passing a string from the command line

This is an example that I created to demonstrate bizarre behavior. I was expecting bash to pass a command line argument as indicated.

john@doe:~/tmp$ cat script.sh 
#! /bin/bash
set -o xtrace
$1 sleep 3
john@doe:~/tmp$ ./script.sh "echo"
+ echo sleep 3
sleep 3
john@doe:~/tmp$ ./script.sh "echo -n"
+ echo -n sleep 3
sleep 3john@doe:~/tmp$

But sometimes bash edits my string argument and adds ticks around characters like; or && </p>

john@doe:~/tmp$ ./script.sh "echo hello ;"
+ echo hello ';' sleep 3
hello ; sleep 3
john@doe:~/tmp$ ./script.sh "echo hello && "
+ echo hello '&&' sleep 3
hello && sleep 3

What is this bash rule and how to get around it? many thanks:)

+4
source share
2 answers

The manual describes the rule: Work with the partition shell .

You will read:

The following is a brief description of a shell operation when it reads and executes a command. Basically, a shell performs the following actions:

, script:

#! /bin/bash

$1 sleep 3

./script "echo hello ;"?

  • Bash script.
  • . , Bash ;, echo hello ;. $1, sleep 3.
  • . .
  • : $1 echo, hello ; ( , , , 2 ).
  • .
  • Bash : echo hello, ;, sleep 3. hello ; sleep 3.
  • ( ) echo.

: - eval, : script

#!/bin/bash

eval "$1 sleep 3"

script:

$ ./script "echo hello;"
hello
$

( 3 ). ./script "echo hello &&". . .

, ( , Bash) : (), . . , , - , , , , "". , *, ./script "echo \"*\";". .

. :

say_hello_and_execute() {
    echo hello; "$@"
}

: export -f say_hello_and_execute script: ./script say_hello_and_execute.

script: script say_hello_and_execute:

#!/bin/bash

echo hello; "$@"

chmod +x say_hello_and_execute script ./script ./say_hello_and_execute.


. , , , , .

+1

, , . , &&. .

echo hello '&&'

echo hello &&

, . , , , , , , , bash .

+1

All Articles