-1

I've been trying to insert apostrophe (') at starting and end in one of my variable, (read variable) but not getting success.

Code:

read message
ping  \'$message\'

Output:

bash test.sh
55 55 
ping: 55': Name or service not known

What I expected: Whenever I enter

ping '55 55'

it shows ping 55 55 : Name or service not known, not like above output (55'). Also when I use echo instead of ping it works correctly; i.e.

echo '55 55'
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I've read your question and I'm still confused about what it is you are trying to do. `ping 55 55` isn't going to do much as `"55 55"` isn't going to be a valid hostname resulting in the error `"Name or service not known"`, as you have found. Please edit your question and explain further what you are trying to accomplish. – David C. Rankin Mar 02 '21 at 05:11

2 Answers2

0

This produces what you wish to see.

read message
ping "${message}"
Vivek Nathani
  • 64
  • 1
  • 5
0

It's unclear why you think adding literal single quotes around a host name would be useful; host names do not have single quotes in them.

ping "$message"

passes the string in message as a single argument to ping.

echo '55 55'

quotes the string 55 55 as a single string, but echo is not very good at showing you what it was actually passed as arguments. Probably a better tool to test with is printf. Compare

bash$ message='55 55'
bash$ printf ">>%s<<\n" $message
>>55<<
>>55<<
bash$ printf ">>%s<<\n" "$message"
>>55 55<<
bash$ printf ">>%s<<\n" \'$message\'
>>'55<<
>>55'<<
bash$ printf ">>%s<<\n" '$message'
>>$message<<
bash$ printf ">>%s<<\n" "'$message'"
>>'55 55'<<
bash$ ping "'55 55'"
ping: cannot resolve '55 55': Unknown host
bash$ ping -c 2 '55 55'
PING 55 55 (0.0.0.55): 56 data bytes
ping: sendto: No route to host
Request timeout for icmp_seq 0

--- 55 55 ping statistics ---
2 packets transmitted, 0 packets received, 100.0% packet loss
bash$ ping 55 55
usage: ping [-AaDdfnoQqRrv] [-c count] [-G sweepmaxsize]
            [-g sweepminsize] [-h sweepincrsize] [-i wait]
            [-l preload] [-M mask | time] [-m ttl] [-p pattern]
            [-S src_addr] [-s packetsize] [-t timeout][-W waittime]
            [-z tos] host
       ping [-AaDdfLnoQqRrv] [-c count] [-I iface] [-i wait]
            [-l preload] [-M mask | time] [-m ttl] [-p pattern] [-S src_addr]
            [-s packetsize] [-T ttl] [-t timeout] [-W waittime]
            [-z tos] mcast-group
Apple specific options (to be specified before mcast-group or host like all options)
            -b boundif           # bind the socket to the interface
            -k traffic_class     # set traffic class socket option
            -K net_service_type  # set traffic class socket options
            -apple-connect       # call connect(2) in the socket
            -apple-time          # display current time

In summary, you need to understand the difference between syntactic quotes (quoting which you use to indicate to the shell that something is a single string) and literal quotes (characters which are part of a string itself, and do not quote anything from the shell), and probably also the difference between single quotes (syntactic quotes which preserve the string within them completely verbatim) and double quotes (syntactic quotes which group the string as one argument, but allow the shell to perform backslash processing, variable interpolation, etc on the value).

tripleee
  • 175,061
  • 34
  • 275
  • 318