3
4
I want to pass multiple files as parameters of an .sh script in Windows 10 by simply dragging them over the script file; is this possible?
3
4
I want to pass multiple files as parameters of an .sh script in Windows 10 by simply dragging them over the script file; is this possible?
1
Windows 10 (v1607 and onward) still doesn't provide this feature by default.
Luckily you can enable it with a registry key (provided below)
The key does of course enable associating .sh script to bash.exe, thus also enables simply double-clicking the script
https://www.codeproject.com/Articles/1214365/Proper-Bash-scripting-on-Windows-Associate-SH-scri (UPDATE: added new icon options for Ubuntu, OpenSUSE and legacy install)
The key also enables the option to run the script in user and elevated mode (the former by double clicking, the latter is a right click option), while an extra (optional) key enables right click > edit with nano
Remember you first have to have install Windows Subsystem for Linux
and set it as default program to open .sh files (the path of the program is C:/Windows/System32/bash.exe)
The main registry key executes the following script
#This makes bash.exe silently execute the command below (whole code)
"%SYSTEMROOT%\System32\bash.exe" -c
#Gets all file paths without expansion/substitution
read -r -d '' path_param <<'EOF'
%*
EOF
read -r -d '' path_exec <<'EOF'
%L
EOF
#Parses all dragged files' paths from Windows paths to unix paths
path_param=$(echo $path_param | tr -d '"' | sed 's/[[:space:]]\([A-Z]:\)/\n\1/g' | sed 's/[A-Z]:/\/mnt\/\L&/g' | tr '\\' '\/'\');
mapfile -t path_param <<< "$path_param";
path_param=("${path_param[@]//:}");
#Same, but with the .sh script path
path_exec=$(echo $path_exec | sed 's/[[:space:]]\([A-Z]:\)/\n\1/g' | sed 's/[A-Z]:/\/mnt\/\L&/g' | tr '\\' '\/'\');
path_exec="${path_exec//:}";
#Sets working directory to the folder where the script is located
cd "${path_exec%\/*}";
#Executes script with or without parameters
if [[ ${path_param[@]} == "" ]];
then "$path_exec";
else "$path_exec" "${path_param[@]/#${path_exec%\/*}\/}";
fi;
#Leaves WSL console open after the .sh script finishes executing
cd ~; bash;
This key should work for both the old and new version of WSL (if the executable path is still C:/Windows/System32/bash.exe; can you confirm? I still haven't received update 1709) – Alex Sim – 2017-11-02T15:56:28.627
@Ramhound by the way, I've done some google search and found this MS page https://msdn.microsoft.com/en-us/commandline/wsl/faq where it says the WSL executable is still called Bash.exe, if the file is also still located in System32, the key would most likely work on WSL too; Again, I'm just supposing here, I guess I'll install the update via Media Creation Tool and try it
– Alex Sim – 2017-11-02T16:20:24.617@Ramhound sorry, that was a poor word choice. What I meant is Bash/WSL still doesn't provide (or has ever provided) this feature; what the key does is silently executing a bash script to parse the windows file paths to their corresponding Linux paths and pass them as parameters of the script – Alex Sim – 2017-11-02T16:30:31.007
@Ramhound I just found this https://medium.com/@finnzeit/set-and-use-zsh-as-default-shell-in-wsl-on-windows-10-the-right-way-4f30ed9592dc ; apparently (from my understanding) there still is bash.exe executable, but there is also a wsl.exe executable which automatically runs the default shell; in this case, my registry key would still work, but could be improved to work with the new wsl.exe file
– Alex Sim – 2017-11-02T17:23:49.787I will leave it to you to adjust your answer accordingly. I will be removing my comments since we resolved my confusion. – Ramhound – 2017-11-02T17:25:26.753
Please post the answer here, not just a link. – Scott – 2017-11-04T15:51:16.277
1
Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places.
– DavidPostill – 2017-11-04T20:25:47.717
3
Possible duplicate of Associate SH scripts to Windows bash
– Ramhound – 2017-11-07T13:55:01.640@Ramhound while the question in this link https://superuser.com/questions/1260528/associating-linux-shell-sh-scripts-on-windows-10-to-bash-or-wsl?noredirect=1#comment1864934_1260528 may be considered a duplicate, I disagree with this one (The drag and drop one) being a duplicate: the solution might be the same, but the problem the user searching it is looking to solve is different, and could not find it without this specific question
– Alex Sim – 2017-11-07T14:24:04.120If an answer to an existing question, answers another a new question as moderators have explained, then that question can be considered a duplicate of another question. I am just doing what that moderator cannot do, because if they voted to close this question, it would be binding. The dilemma you have placed the community in, is the reason, duplicate answers shouldn't be submitted to multiple questions. – Ramhound – 2017-11-07T14:41:26.987