1

The terminal on my VPS originally looked like this in PuTTY:

[root@user ~]#

Then now it looks like this:

-bash-4.1#

This is the error that now shows up:

-bash: /root/.bash_profile: line 6: syntax error near unexpected token `fi'
-bash: /root/.bash_profile: line 6: `fi'

I don’t know what happened to cause this.

How do I get back to expected [root@user ~]#?

The /root/.bash_profile looks like this inside:

PuTTY screenshot of the contents of “/root/.bash_profile”

14
  • 2
    Fix .bash_profile. We can't tell you what's wrong with it as you don't include it in your question.
    – DavidPostill
    Commented Aug 15, 2018 at 19:43
  • Hello @DavidPostill, ok have edited the question adding the .bash_profile print, although it doesn´t look any different than when it was originally.
    – Joao
    Commented Aug 15, 2018 at 19:51
  • Missing colon --> . ~/.bashrc;?
    – DavidPostill
    Commented Aug 15, 2018 at 19:57
  • Then you must have some other problem character in there (unicode character?, wrong EOL character?). Try retyping the if ... fi lines and deleting the existing ones. Is there a stray ' in there? (the question mentions fi')
    – DavidPostill
    Commented Aug 15, 2018 at 20:10
  • @David the ; didn´t make any change, there is no ' in the file nor any fi', it looks like the image. I have updated the file with same things and remains the same problem.
    – Joao
    Commented Aug 15, 2018 at 20:11

3 Answers 3

1

What caused the problem versus how to solve it are two different things. What I do know this these are the contents of a fairly standard Bash .bash_profile file; via RedHat 7:

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH

And these are the contents of a fairly standard Bash .bashrc file; via RedHat 7:

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

Also, the contents of /etc/bashrc on RedHat 7 are as follows:

# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# are we an interactive shell?
if [ "$PS1" ]; then
  if [ -z "$PROMPT_COMMAND" ]; then
    case $TERM in
    xterm*|vte*)
      if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
      elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
          PROMPT_COMMAND="__vte_prompt_command"
      else
          PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
      fi
      ;;
    screen*)
      if [ -e /etc/sysconfig/bash-prompt-screen ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
      else
          PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
      fi
      ;;
    *)
      [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
      ;;
    esac
  fi
  # Turn on parallel history
  shopt -s histappend
  history -a
  # Turn on checkwinsize
  shopt -s checkwinsize
  [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
  # You might want to have e.g. tty in prompt (e.g. more virtual machines)
  # and console windows
  # If you want to do so, just add e.g.
  # if [ "$PS1" ]; then
  #   PS1="[\u@\h:\l \W]\\$ "
  # fi
  # to your custom modification shell script in /etc/profile.d/ directory
fi

if ! shopt -q login_shell ; then # We're not a login shell
    # Need to redefine pathmunge, it get's undefined at the end of /etc/profile
    pathmunge () {
        case ":${PATH}:" in
            *:"$1":*)
                ;;
            *)
                if [ "$2" = "after" ] ; then
                    PATH=$PATH:$1
                else
                    PATH=$1:$PATH
                fi
        esac
    }

    # By default, we want umask to get set. This sets it for non-login shell.
    # Current threshold for system reserved uid/gids is 200
    # You could check uidgid reservation validity in
    # /usr/share/doc/setup-*/uidgid file
    if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
       umask 002
    else
       umask 022
    fi

    SHELL=/bin/bash
    # Only display echos from profile.d scripts if we are no login shell
    # and interactive - otherwise just process them to set envvars
    for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            if [ "$PS1" ]; then
                . "$i"
            else
                . "$i" >/dev/null
            fi
        fi
    done

    unset i
    unset -f pathmunge
fi
# vim:ts=4:sw=4

Also note you should only edit files like this with a pure text editor. If you use a word processor or anything “fancier” than that, extra cruft and “gremlins” might be added to your file which would make the system choke on it.

5
  • 1
    Hello Jake, thanks for the reply. I copied your first one to the .bash_profile and unexpected token still happens. I am using linux centos 6-64.
    – Joao
    Commented Aug 15, 2018 at 20:36
  • 1
    @Joao What editor do you using ?
    – Alex
    Commented Aug 15, 2018 at 20:38
  • @Alex, I am altering opening the .bash_profile directly via winscp, however, when this problem happened, I hadnt made any changes to it, the problem occurred suddenly while logging into putty
    – Joao
    Commented Aug 15, 2018 at 20:45
  • 2
    If you used WinSCP and tried to edit it (probably with some windows program) then it highly likely that line ending was broken. Windows will put 0x0D,0x0A while bash expecting line ending as 0x0A only
    – Alex
    Commented Aug 15, 2018 at 20:47
  • @Joao "directly via winscp" – This looks like an oxymoron to me. :) Commented Aug 15, 2018 at 20:50
1

Remove ~/.bash_profile and add content below to ~/.profile
it would work with other shells too.

# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

export PATH="${PATH}:${HOME}/bin"

Make sure to remember, how different shells processing start up files.
Here is diagram.

If you using any windows programs to edit content of script files or configuration files on Unix based computers, you should always keep in mind that line ending in those two operation system are different. On Linux, end of line - it is character 0x0A. Either use editors that supports switching between Unix and Windows line ending, such as Notepad++ or use ported to Windows vim editor.

4
  • Hello @Alex, made what you suggested but still appears the unexpected token with the -bash-4.1#
    – Joao
    Commented Aug 15, 2018 at 20:43
  • @Joao If you can use WinSCP, then you can probably login over ssh too. Login to your box via ssh and edit content on ~/.profile with nano directly on remote computer. Run when you logged: nano ~/.profile. If you haven't access to remote box over ssh, then use notepad++ for editing but make sure you changed line ending (!!!) to Unix.
    – Alex
    Commented Aug 15, 2018 at 20:49
  • 1
    "If you haven't access to remote box over ssh" – Read the question. :) The OP runs Bash there, full access. Commented Aug 15, 2018 at 20:56
  • @KamilMaciorowski A huge + for that ! You right as always :) Oops moment...
    – Alex
    Commented Aug 15, 2018 at 21:03
0

As Alex mentioned, you may have inadvertently created an end of line problem. Assuming you have the utility in place try:

dos2unix /root/.bash_profile

This will convert the new line characters.

2
  • Hello @user10216038, have tried and it returns this: -bash: dos2unix: command not found
    – Joao
    Commented Aug 15, 2018 at 21:46
  • Add the dos2unix utilty if you can. Alternatively you can try using the strings command to do something similar: "strings /root/.bash_profile > tempbash.txt" . This will change the windows 0D0A returns to 0A but it will also strip out consecutive returns so isn't ideal. Compare the resultant tempbash file carefully and copy it back to bash_profile if it looks right. Commented Aug 18, 2018 at 20:53

You must log in to answer this question.

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