Script failing with “command not found: ^M”

When I try to run the following script in zsh, via the command /bin/zsh ~/.set_color_scheme.sh I get the following error:

command not found: ^M

The script has u+x permissions and it used to work on another machine that also had zsh. Any clues why?

Note: This question is related to this other question (where I am trying to adapt the script for tcsh)

#!/bin/zsh
# Contents of set_color_scheme.sh

export LS_COLORS=$( 
( grep 'w' | grep -v '^#' | sed 's/#.+//' | perl -lane 'printf "%s=%s:", shift @F, join ";", @F;' ) <<< "

# HUMAN_FORMATTED_DATA
# list one per line

# these are basic filesystem items
no 00          # normal
fi 00          # file
di 01 34       # directory
ln 00 36       # link
pi 40 33       # pipe
so 00 35       #
bd 40 33 01
cd 40 33 01
or 01 05 37 41
mi 01 05 37 41
ex 00 91       # executable
ow 01 34       # other writables


*.cmd 00 32
*.exe 00 32

# archive, compressed things etc
*.gz  00 90
*.bz2 00 90
*.bz  00 90
*.tz  00 90
*.rpm 00 90
*.rar 00 90
*.zip 00 90
*.iso 00 90


*.cpio 00 31



# perl & CODE
*.c      33
*.h      33
*.sh     33
*.t      33
*.pm     33
*.pl     33
*.cgi    33
*.pod    33
*.PL     33
*.js     33
*.php    33
#*.xs

# strikethrough
*.off 00 9
*.bak 00 9
*.old 00 9


# documents misc, html webstuff
# really TEXT
*.htm    94
*.html   94
*.txt    94
*.text   94
*.css    94


# MOVIE
*.avi    96
*.wmv    96
*.mpeg   96
*.mpg    96
*.mov    96
*.AVI    96
*.WMV    96
*.mkv    96

# images & pdf
*.jpg    96
*.jpeg   96
*.png    96
*.xcf    96
*.JPG    96
*.gif    96
*.svg    96
*.eps 00 96
*.pdf 00 96
*.PDF 00 96
*.ps  00 96

*.ai  00 91 # adobe ill
*.doc 00 91 # msword 

# data, such as .db, .csv
*.csv    95
*.dsv    95
*.db     95
*.sql    95
*.meta   95
# CONFS
*.xml    95
*.yaml   95
*.yml    95
*.conf   95
# [a-z0-9]*rc
")

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your shell script is in DOS/Windows text format (with CR+LF). Convert it to Unix format (e.g. use dos2unix).

This won’t work as you want though, as explained in the linked question. A new shell is executed, the variable it set, then the shell ends, taking the variable with it.

If you have a script that uses the same shell as the one you’re running (or has compatible syntax), then you can execute the script by using . or source. This way the script is run as if you typed it all in yourself, so all variables remain (and it will ignore the #! line).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x