20

I created basic script in Windows.

#!/bin/bash
echo Hello

I am using Cmder, ConEmu derivative. I tried to change the priviliges with chmod, but they are the same. I don't know how can I execute this script. Normal Linux way, which is: ./hello.sh does not work, and typing just hello.sh makes Windows try to open it, which is bad, since I want it in console. How to execute this script in ConEmu/Cmder?

asked Mar 24, 2016 at 8:54
2
  • 4
    Windows does not support the #! line, so you need to execute the program, e.g. bash hello.sh (as you can in Linux), or change the file association for the .sh extension - see the stat and file commands. Commented Mar 24, 2016 at 9:02
  • Thank you, that will do. Commented Mar 24, 2016 at 9:03

6 Answers 6

17

I've noticed you can run bash from cmder. So I could do it like:

> bash
$ ./yourScript.sh

or simpler

> cat yourScript.sh | bash

Disclaimer: New to cmder (just downloaded it) and Linux myself.

answered Jul 11, 2016 at 23:03
Sign up to request clarification or add additional context in comments.

Comments

12

It works just like on Unix shells

sh path/to/your/script.sh
answered Mar 17, 2019 at 11:08

2 Comments

This worked best for me - sh runs the script in your current environment (e.g. conda environment) while bash opens a new shell.
Thank you, this worked for me: I'm on Windows 11 and wasn't able to run "bash myscript.sh" anymore because I kept receiving errors on part of the script that were working before.
11

On my own instance of Cmder, bash [filename] works just fine, and I believe much simpler:

C:\Users\Conor O'Brien
λ type test.sh
echo Hello
C:\Users\Conor O'Brien
λ bash test.sh
Hello
answered Mar 12, 2017 at 22:21

Comments

6

If you want to be able to run the script by simply typing its name, a workaround is to create an alias and put it into your .bashrc such as:

alias scriptName="bash /pathToTheScript/yourScript.sh"

Or you can source a script inside your .bashrc and make it available through a function:

source /pathToTheScript/yourScript.sh

where the script is:

#!/bin/bash
function your_function()
{
yourCode
}
answered Sep 22, 2017 at 12:39

Comments

1

If you don't have time jump to The conclusion below:

TL:DR: Here my toying with "Cmder> bash" on Windows to create a Global script:

I've created a external script:

a@DESKTOP /c/Scripts/
λ vi test.sh

with the content

#!/bin/bash
echo 'Can you see me now?'

it can be executed from the same folder:

a@DESKTOP /c/Scripts/
λ ./test.sh
Can you see me now?

on creating a simbolink link:

λ ln -s /c/Portables/Scripts/GlobalesBash/test.sh /bin/mytest

it seems to work fine calling it with just the name:

λ mytest
Can you see me now?

but if the original file gets modified:

λ cat test.sh
#!/bin/bash
echo 'Yes, I see you'

the changes are not reflected using the link:

λ mytest
Can you see me now?

The conclusion:

so the best option is creating the script directly on the folder /bin:

λ cd /bin
λ vi aloha
λ cat aloha
echo 'aloha!!!'

and #!/bin/bash isn't even necessary with Cmder on Windows, and it gets executed successfully from everywhere in the Cmder bash:

λ cd /c
a@DESKTOP /c
λ aloha
aloha!!!
answered Mar 16, 2019 at 16:20

Comments

0

You can put your own .sh files into $CMDER_ROOT/config/profile.d/*.sh directory, as the docs explain here

answered Mar 31, 2020 at 20:25

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.