0

I'm new to Linux. I'm trying to make a script that prints something and initiates a shutdown command. This is my script:

#!/bin/bash    

echo Hello WOOOOORRRRRLLLLDDDDDDD

echo sudo poweroff

Every time I try to run my script,using

chmod +x Hello World BASH

or ./Hello World BASH

however, I get this error message:

chmod: cannot access 'Hello': No such file or directory
chmod: cannot access 'World': No such file or directory
chmod: cannot access 'BASH': No such file or directory

In general, everytime I try to run this script, the "no such file or directory" error message popped up. How can I fix this?

2 Answers2

1

Because the file name has spaces in it, you need to use quotes or escapes so the space does not make it look like 3 different file names:

chmod a+x "Hello World BASH"
"./Hello World BASH"

Or

chmod a+x Hello\ World\ BASH
./Hello\ World\ BASH
psusi
  • 37,551
0

Short answer

Run these commands in this order:

chmod +x "Hello World BASH"
./"Hello World BASH"

Long answer

A filename with spaces has to be quoted (with single or double quotes) or escaped (with backslashes) so that the shell understands that it's all part of the filename. Otherwise, it will break on the spaces and interpret one filename (Hello World BASH) as three filenames (Hello, World, and BASH)

Also chmod +x does not run the script, it makes the script executable.

By the way, if you don't have have execute permissions for a given file, you can call the interpreter to run it:

bash "Hello World BASH"
wjandrea
  • 14,236
  • 4
  • 48
  • 98