Execute Shell Script in Linux Server Without Causing Error
In this article, we are going to learn how to execute a shell script in Linux server.
Create Shell on Server & Run
We can easily create and run a shell script from SSH. Now we will create a shell script which will show the current date. Let’s see the procedure:
1. Create a new file called date.sh. We can make the file by using sudo nano date.sh
command.
2. Add this code:
#!/bin/bash
# My first shell script
echo "Today is $(date)"
3. Make the shell script executable by hitting this command:
chmod +x date.sh
Our shell is ready to run. Let’s run by this command:
./date.sh

Upload Shell on Server & Run
We have created and run a shell script from the server. Now we will upload a shell to the server and then will run.
Create a file called date.sh
using any text editor (Notepad, Sublime etc.). Then copy-paste the previous shell script and save the file.
After saving to local, upload it to the server using FTP. After doing this, let’s make shell script executable:
chmod +x date.sh
Now try to run the file:
./date.sh
If it works, then okay. But if it doesn’t work, you have to do an extra thing. You can get an error like this:
/bin/bash^M: bad interpreter: No such file or directory
To solve this error, we can follow two methods.
Method 1
Run this command to fix the error and make a correct shell file:
cat date.sh | tr -d '\r' > date.sh.new
You will see a file called date.sh.new. Delete the date.sh and rename the date.sh.new to date.sh.
Now try to run the script. It should work.
Method 2
Install dos2unix:
sudo yum -y install dos2unix
Then run this command to convert the file to Unix format:
sudo dos2unix date.sh
Now run the script. It should run.
We are done. Thanks for reading. ?Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)