Wednesday, October 27, 2021

[SOLVED] Why am I getting an if: Expression syntax error in ~/.profile?

Issue

I am getting an 'if: Expression syntax' error in my .profile when I try to source ~/.profile it. What am I doing wrong? .profile file


Solution

The code from the link looks 'ok', runs 'ok' in my bash environment, and checks out as 'ok' @ ShellCheck.

From OP's comments the following does not generate a syntax error:

$ bash
$ source ./.profile
$                         <<<=== no error message, just a command prompt

This confirms the contents of the OP's .profile is 'ok' for a bash environment.

From additional comments we're told:

$ echo $SHELL
/bin/tcsh

Herein lies the issue ... tcsh is not the same as bash, which also means some of the commands in the OP's .profile are invalid when executed under the tcsh shell.

We can confirm this if we go out to Try It Online, scroll down and choose tcsh, enter the following code ...

tty -s
if test $? = 0
then
    stty dec crt
fi

... and when we run the code we get:

if: Expression Syntax.

To get around this syntax issue there are a couple options:

  • the OP can get the default shell switched to bash (see chsh or have the sysadmin update /etc/passwd to replace the tcsh shell with the bash shell); once this is accomplished all new login/shell sessions should have no problems sourcing the OP's .profile
  • or ...
  • the OP can manually run bash at the command line and then manually source the .profile


Answered By - markp-fuso