How to pass path from shell read command to wslpath


Edwin Bradford

I'm trying to write a script that, wslpathwhen run in WSL on Windows, will pass the tool the location of a directory dropped into a bash window .

# Drag and drop the directory into the Bash window
IFS="" read -r input
echo;

# If system is Windows Subsystem for Linux convert Windows paths to Unix paths
if [[ $(uname -r) =~ Microsoft$ ]]; then
    input="$( wslpath "$input" )"
fi

# Use eval to parse spaces in the path as an array
eval "input=( $input )"

# List files and directories
ls -l "${input}"; echo;

If there are spaces in the directory name, the directory to list cannot be found.

If I move the line eval "input=( $input )"above the Windows Subsystem for Linux, it will find the directory if there is a space in the name, but not if there is no space.

If you replace the line input="$( wslpath "$input" )"with the following Bash replacement expression , you can use spaces or no spaces, but the drive letter must be hardcodedinput=${input/F:\\/\/mnt\/f\/}

I've read multiple threads about quote evaluation order in Bash, passing arguments to functions as arrays, and then expanding on those arrays, but I'm really struggling with this and I can't get it to work.

Sokovy

Since there is only one path, there is no need for an array. Spaces within paths are handled correctly as long as the variable is quoted.

In fact, splitting the path into an array of words results in a loss of information. You won't know if the array (a b)was originally a b(one space) or a b(two spaces). Also, you don't need evalto split the variable into words. array=($var)is the way to go.

The problem here is that Windows pastes paths with spaces by enclosing the path in double quotes. That is, if you drag and drop you C:\aget strings C:\a, but if you drag and drop you C:\a bget strings "C:\a b". These quotes are not interpreted by bash because they are only input, not part of the script. wslpathNor will these references be interpreted. Instead, it looks for a file wslpath '"C:\a b"'that is actually a b"on drive "C:. We had to manually remove the quotes inserted by Windows' "drag and drop".

IFS= read -r input; echo
if [[ "$(uname -r)" =~ Microsoft$ ]]; then
    # is path quoted and contains a space?
    if [[ "$input" =~ ^\"(.*\ .*)\"$ ]]; then
      # remove quotes
      input="${BASH_REMATCH[1]}"
    fi
    input="$( wslpath "$input" )"
fi
ls -l "${input}"; echo

Related


How to pass path from shell read command to wslpath

Edwin Bradford I'm trying to write a script that, wslpathwhen run in WSL on Windows, will pass the tool the location of a directory dropped into a bash window . # Drag and drop the directory into the Bash window IFS="" read -r input echo; # If system is Windo

How to pass path from shell read command to wslpath

Edwin Bradford I'm trying to write a script that, wslpathwhen run in WSL on Windows, will pass the tool the location of a directory dropped into a bash window . # Drag and drop the directory into the Bash window IFS="" read -r input echo; # If system is Windo

How to pass path from shell read command to wslpath

Edwin Bradford I'm trying to write a script that, wslpathwhen run in WSL on Windows, will pass the tool the location of a directory dropped into a bash window . # Drag and drop the directory into the Bash window IFS="" read -r input echo; # If system is Windo

How to pass path from shell read command to wslpath

Edwin Bradford I'm trying to write a script that, wslpathwhen run in WSL on Windows, will pass the tool the location of a directory dropped into a bash window . # Drag and drop the directory into the Bash window IFS="" read -r input echo; # If system is Windo

How to pass path from shell read command to wslpath

Edwin Bradford I'm trying to write a script that, wslpathwhen run in WSL on Windows, will pass the tool the location of a directory dropped into a bash window . # Drag and drop the directory into the Bash window IFS="" read -r input echo; # If system is Windo

How to pass variables from Jenkinsfile to shell command

Joe: I would like to use a variable inside a Jenkinsfilescript and then pass its value to shell script execution (either as an environment variable or command line argument). But the following Jenkinsfile: for (i in [ 'a', 'b', 'c' ]) { echo i sh 'echo

How to pass variables from awk to shell command?

Vahid Mirjalili I'm trying to run a shell command from awk for each line of a file that requires an input parameter. I tried to use system()but the input parameter is not recognized. Each line of this file is the address of a file that I want to run a command

How to pass variables from awk to shell command?

Vahid Mirjalili I'm trying to run a shell command from awk for each line of a file that requires an input parameter. I tried to use system()but the input parameter is not recognized. Each line of this file is the address of a file that I want to run a command

How to pass variables from Jenkinsfile to shell command

Joe: I would like to use a variable inside a Jenkinsfilescript and then pass its value to shell script execution (either as an environment variable or command line argument). But the following Jenkinsfile: for (i in [ 'a', 'b', 'c' ]) { echo i sh 'echo

How to convert wslpath /home/user/ to windows path

User 504909 I'm using the Linux Subsystem for Windows 10 (Windows 10 version 1803) I can use the command line: user@laptop:~$ wslpath -w /c/ C:\ but when I try to use user@laptop:~$ wslpath -w ~ wslpath: /home/user: Result not representable Even if I use: us

Pass file path to shell command in vim

David 542 How would I properly "escape" the following command in vim? :!file=expand('%:r') Basically, I want to do something like: $ file=my_filename This way I can execute subsequent shell commands to quote $file. Currently I get this error: Press ENTER or

How to pass commands from shell script to command line

FERÿ I am using ssh on linux to remotely access a second linux. What ssh does is make Linux's Terminal the remote Linux's Terminal, and anything I type in Terminal will be executed on the remote Linux. Now I want to use shell script to enter commands after est

How to pass commands from shell script to command line

FERÿ I am using ssh on linux to remotely access a second linux. What ssh does is make Linux's Terminal the remote Linux's Terminal, and anything I type in Terminal will be executed on the remote Linux. Now I want to use shell script to enter commands after est

How to pass commands from shell script to command line

FERÿ I am using ssh on linux to remotely access a second linux. What ssh does is make Linux's Terminal the remote Linux's Terminal, and anything I type in Terminal will be executed on the remote Linux. Now I want to use shell script to enter commands after est

How to read the output of a shell command

Vivek I'm trying to run a shell script and based on the output determine what node-1 and node-2 are. Currently I am able to get the desired output of the shell command, but not sure how to sort/read it and get the node names. import subprocess cmd = ["olsnodes

Pass arguments with spaces to command from shell script?

Adi Gerber See edit below, thanks I have the following test script ( IMPORTANT: I cannot change this part ): while (($#)); do echo $1 shift done Running the command ./test aaa "bbbb cccc" dddwill give the following output: aaa bbbb cccc ddd This is what

Pass arguments with spaces to command from shell script?

Adi Gerber See edit below, thanks I have the following test script ( IMPORTANT: I cannot change this part ): while (($#)); do echo $1 shift done Running the command ./test aaa "bbbb cccc" dddwill give the following output: aaa bbbb cccc ddd This is what

read python from shell command output

high mark I understand you might think this is a duplicate question, but so far I haven't found a solution to my own problem. Currently, I have a C program that constantly produces a stream of data. I want my python program to have a way to read this data so I

Shell script: read input from command output

Rahul Why does the following code only give the first line of ps -eaf output in ps.out? while read line; do echo $line>ps.out; done < <(/bin/ps -eaf) Nicola You truncate the file every time, so you only get the last line. You might want to >>instead >.

read python from shell command output

high mark I understand you might think this is a duplicate question, but so far I haven't found a solution to my own problem. Currently, I have a C program that constantly produces a stream of data. I want my python program to have a way to read this data so I

Shell script: read input from command output

Rahul Why does the following code only give the first line of ps -eaf output in ps.out? while read line; do echo $line>ps.out; done < <(/bin/ps -eaf) Nicola You truncate the file every time, so you only get the last line. You might want to >>instead >.

Pass the variable read from the file to the calling command

Adam Lacy I'm trying to recycle all IIS app pools remotely using powershell with the word "Test" in the name, but also excluding a few specific AppPools with Test in the name. I can use locally: ## List of Apppool Names to Exclude $Exclusions = Get-Content "C:

Read from standard input and pass to next command

Christianity I want to read the password from stdin, suppress the input and encode it with base64 like this: read -s|openssl base64 -e What is the correct command? wind The read command sets the bash variable, but does not output to stdout. For example put st

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

How to change PATH for Makefile $(shell...) command?

user 541686 when i run export PATH := mypath $(error $(shell echo "$${PATH}")) Nothing seems to PATHchange for my call shell. Why is this happening and how do I actually change the PATHfor shellcall? Florian Weimer Is this GNU make? There's a long-standing GN

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'

How to change PATH for Makefile $(shell...) command?

user 541686 when i run export PATH := mypath $(error $(shell echo "$${PATH}")) Nothing seems to PATHchange for my call shell. Why is this happening and how do I actually change the PATHfor shellcall? Florian Weimer Is this GNU make? There's a long-standing GN

Snakemake: How to specify absolute path to shell command

Vkkodali I'm writing a snakemake rule that uses multiple commands like this: rule RULE1: input: 'path/to/input.file' output: 'path/to/output.file' shell: 'path/to/command1 {input} | /path/to/command2 | /path/to/command3 {output}' If /path/to/command1it'