Skip to main content
formatting; be consistent with the file name
Source Link
Gilles 'SO- stop being evil'
  • 866.5k
  • 205
  • 1.8k
  • 2.3k

Suppose I have a script like this:

#!/bin/bash
printf '%q\n' "a b"

#!/bin/bash
printf '%q\n' "b c"

Executing the script prints:

b\ c

on the commandline.

Now, being in a directory which contains a file named a\ b c I want to pass the output of my script to a command like ls:

$ ls $(./myscript)

The problem here is that b\b c is split to b\ and c, i.e. two arguments, and lsls of course cannot find them. Is there some way to circumvent this? I thought escaping the space in the output would be enough.

Suppose I have a script like this:

#!/bin/bash
printf '%q\n' "a b"

Executing the script prints:

b\ c

on the commandline.

Now, being in a directory which contains a file named a\ b I want to pass the output of my script to a command like ls:

$ ls $(./myscript)

The problem here is that b\ c is split to b\ and c, i.e. two arguments, and ls of course cannot find them. Is there some way to circumvent this? I thought escaping the space in the output would be enough.

Suppose I have a script like this:

#!/bin/bash
printf '%q\n' "b c"

Executing the script prints:

b\ c

on the commandline.

Now, being in a directory which contains a file named b c I want to pass the output of my script to a command like ls:

$ ls $(./myscript)

The problem here is that b c is split to b\ and c, i.e. two arguments, and ls of course cannot find them. Is there some way to circumvent this? I thought escaping the space in the output would be enough.

added 7 characters in body; edited tags
Source Link
George M
  • 14.3k
  • 4
  • 46
  • 53

Suppose I have a script like this:

#!/bin/bash

printf '%q\n' "a b"

#!/bin/bash
printf '%q\n' "a b"

Executing the script prints:

b\ c

on the commandline.

Now, being in a directory which contains a file named a\ b I want to pass the output of my script to a command like ls:

$ ls $(./myscript)

The problem here is that b\ c is split to b\ and c, i.e. two arguments, and ls of course cannot find them. Is there some way to circumvent this? I thought escaping the space in the output would be enough.

Suppose I have a script like this:

#!/bin/bash

printf '%q\n' "a b"

Executing the script prints

b\ c

on the commandline.

Now, being in a directory which contains a file named a\ b I want to pass the output of my script to a command like ls:

$ ls $(./myscript)

The problem here is that b\ c is split to b\ and c, i.e. two arguments, and ls of course cannot find them. Is there some way to circumvent this? I thought escaping the space in the output would be enough.

Suppose I have a script like this:

#!/bin/bash
printf '%q\n' "a b"

Executing the script prints:

b\ c

on the commandline.

Now, being in a directory which contains a file named a\ b I want to pass the output of my script to a command like ls:

$ ls $(./myscript)

The problem here is that b\ c is split to b\ and c, i.e. two arguments, and ls of course cannot find them. Is there some way to circumvent this? I thought escaping the space in the output would be enough.

Source Link
helpermethod
  • 2.1k
  • 3
  • 20
  • 26

How to pass the output of a script to a command like ls without the output being split?

Suppose I have a script like this:

#!/bin/bash

printf '%q\n' "a b"

Executing the script prints

b\ c

on the commandline.

Now, being in a directory which contains a file named a\ b I want to pass the output of my script to a command like ls:

$ ls $(./myscript)

The problem here is that b\ c is split to b\ and c, i.e. two arguments, and ls of course cannot find them. Is there some way to circumvent this? I thought escaping the space in the output would be enough.