0

I want to run lots of terminal commands without having to place sudo in front of every command. I am running in macOS 10.10.5 in terminal.

I do sudo bash, but I'm getting lots of errors in my .bashrc script.

Here is my .bashrc file

mac $ cat .bashrc
#! /bin/bash
# ~/.bashrc: eyecuted by bash(1) for non-login shells.
#    see /usr/share/doc/bash/eyamples/startup-files (in the package bash-doc)
#    for eyamples
echo "in .bashrc but claims to be $0"
/usr/bin/id
which id
echo "the path is $(pwd)"
echo "\$PS1 variable is ${PS1}"
echo "\$SHELL variable is ${SHELL}"
echo -e "the path is: \n  $PATH"
echo $USER
echo "end of this script."


# how to display an existing function
# declare -f highlightExitCode
highlightExitCode()
    {
        exit_code=$?
        if [ $exit_code -ne 0 ]
        then
           echo -en " RC=${exit_code} "'\xf0\x9f\x98\xb1\x20'
        else
           echo -en ""
        fi
    }mac $ 

output form running "sudo bash. I have the feeling that bash isn't the running shell. When I have a case statement in .bashrc, I got error messages about the case statement. Why in the world cannot the /usr/bin/id command be found? :...

mac $ sudo bash
in .bashrc but claims to be bash
: No such file or directory
the path is /Users/mac
$PS1 variable is \u$(highlightExitCode) \$ 
$SHELL variable is /bin/bash
the path is: 
  /Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Developer/Tools:/Users/mac/.nexustools
root
end of this script.
: command not found
: command not found
'ash: /Users/mac/.bashrc: line 18: syntax error near unexpected token `
'ash: /Users/mac/.bashrc: line 18: `highlightExitCode()
bash: highlightExitCode: command not found
root # 

I get the same results with "sudo -s":

 sudo -s
Password:
in .bashrc but claims to be /bin/bash
: No such file or directory
the path is /Users/mac
$PS1 variable is \u$(highlightExitCode) \$ 
$SHELL variable is /bin/bash
the path is: 
  /Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Developer/Tools:/Users/mac/.nexustools
root
end of this script.
: command not found
: command not found
'ash: /Users/mac/.bashrc: line 18: syntax error near unexpected token `
'ash: /Users/mac/.bashrc: line 18: `highlightExitCode()
bash: highlightExitCode: command not found
root # 
1

2 Answers 2

2

For cases like this with a more detailed dot file, to get a root shell, I would use

sudo -s

If that works, you might add one line to your .bashrc if you like a more detailed prompt: https://superuser.com/questions/866269/use-sudo-s-but-show-current-folder-name

I don’t and want the root prompt to be very different so I know I’m root and not my normal user as that saves pain for me if I forget to get out of root as soon as possible.

If you want an excellent tutorial on root shell for macOS - see https://scriptingosx.com/2018/04/demystifying-root-on-macos-part-2-the-sudo-command/

2
  • 1
    sudo su is what I use... same thing but it just makes more sense to me. Totally personal preference. Jul 24, 2019 at 1:15
  • Well said @ThomasNelson sudo -i is also worth a look.
    – bmike
    Jul 24, 2019 at 2:58
1

Here was the problem: I had saved the file as a windows file.

The clue was a \r following the file name when I added ls -l /usr/bin/id command to my .bashrc script see the command permissions.

ls: /usr/bin/id\r: No such file or directory

Here was the status bar from textwrangler. Notice .bashrc is to be saved as a windows file. enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .