To reset the admin password of SolusVM I am executing the following command:
php /usr/local/solusvm/scripts/pass.php --type=admin --comm=change --username=<ADMINUSERNAME>
When doing so, the following error message is printed:
-bash: syntax error near unexpected token `newline'
What is the reason for this problem? I am running the command as root.
![]()
iFreilicht
12.3k6 gold badges41 silver badges73 bronze badges
asked Feb 27, 2011 at 16:36
The characters ‘<‘, and ‘>’, are to indicate a place-holder, you should remove them to read:
php /usr/local/solusvm/scripts/pass.php --type=admin --comm=change --username=ADMINUSERNAME
answered Feb 27, 2011 at 16:40
Terence SimpsonTerence Simpson
4,3412 gold badges20 silver badges18 bronze badges
2
- Печать
Страницы: [1] Вниз
Тема: ошибка синтаксиса около неожиданной лексемы `newline’> (Прочитано 3538 раз)
0 Пользователей и 1 Гость просматривают эту тему.

Артур27
Всем привет! вот такая ошибка вылезла ошибка синтаксиса около неожиданной лексемы `newline’>, как исправить не понимаю, так как неделю всего пользуюсь ноутом, помогите!! крик души

EvangelionDeath
Артур27, а мы должны угадать где вылезла, в следстии чего и что вы перед этим делали?
HP Pro 840 G3: Intel i5-6300U, 32GB DDR4 2133MHz, Intel 520, Intel Pro 2500 180GB/Ubuntu 22.04
Dell Latitude 5590: Intel i5-8350U, 16GB DDR4 2400MHz, Intel 620, Samsung 1TB/Ubuntu 22.04

Артур27
artur@Inspiron-5570:~$ <sudo app-get install-f>
bash: ошибка синтаксиса около неожиданной лексемы `newline’
artur@Inspiron-5570:~$
В правом верхнем углу вылезло уведомление: Произошла ошибка. Пожалуйста запустите менеджер пакетов или введите команду в терминале sudo app-get install-f.
наверху пример, ввожу и получаю такую ошибку

ALiEN175
скобки уберите
и не app-get, a apt-get
и пробел после install где?
ASUS P5K-C :: Intel Xeon E5450 @ 3.00GHz :: 8 GB DDR2 :: Radeon R7 260X :: XFCE
ACER 5750G :: Intel Core i5-2450M @ 2.50GHz :: 6 GB DDR3 :: GeForce GT 630M :: XFCE

Azure
Артур27, странный какой-то пример… Может надо так?
artur@Inspiron-5570:~$ sudo apt-get install -f
В Линукс можно сделать ВСЁ что угодно, достаточно знать КАК !

Артур27
Блин вот я тупил… спасибо! Все верно сказано
- Печать
Страницы: [1] Вверх
When writing code with Bash scripting, special characters or lack of quotation marks may cause errors. This article will explain how to solve the bash: syntax error near unexpected token newline in Linux Bash.
Solve the bash: syntax error near unexpected token newline Error in Linux Bash
There are many code-related reasons why you get a syntax error near unexpected token newline error in Bash scripting. The rest of the article explains the most common mistakes and ways to fix them.
If you are getting this error, you most likely made a syntax error. First, read the file’s contents with the cat command.
Note the use of special characters. The < character is used for input redirection, and the > character is used for output redirection.
If you are using these angle brackets, you must use them in quotes. Otherwise, an error will occur.
For example, the following code will throw the syntax error near unexpected token newline.
#!/bin/bash
str=<you cannot do this>
However, you can use the angle brackets with quotes. The code below will work without errors.
#!/bin/bash
str="<You can do this>"
It does not matter if you use double quotes or single quotes. You can also do it as below.
#!/bin/bash
str='<You can also do this>'
Similarly, you cannot use angle brackets in the Bash command line parameters. Let’s examine the example below.
There is a login program that takes username and password as arguments. If any of the arguments contain an angle bracket, you will get a bash: syntax error near unexpected token newline error.
./login --username admin --password 1234<
Enclose the argument in single or double quotes to avoid an error.
./login --username admin --password "1234<"
If you are using a Bash script or git add command in Linux operating system you can face the error message ‘syntax error near unexpected token `newline’. It occurs when you are missing some angle brackets or missing proper spaces or characters while writing the script. It can be resolved if single or double quotes are used when writing the code.
What Is Bash Syntax Error #
It is an error when you don’t follow the syntax in the right direction in “.sh” files using git add. And if you are missing some quotation marks you will get the error syntax error near the unexpected token `newline’. These quotation marks let the syntax understand an entity with a proper character, writing the Bash syntax script you have to be careful about quotations and angle brackets. The other way result will show you the error.
The Reason It Happens #
This error message comes when some special characters, quotation marks, and writing a Bash script are missing. The Bash syntax error denotes that there is something wrong with the coding or that a bad script is written somewhere in the command. Commonly escaping missing space, characters, or comas.
During the execution process, it is not that easy to find out the syntax error that is syntax error near unexpected token `newline’. It may require you to change your coding many times for resetting the script. You need to take care of writing the code in Bash as it might be difficult to find out the errors easily.
The Solution Methods #
The Bash syntax error may occur in multiple scenarios and it becomes difficult to find out the exact error until you review all of the scripts again. Though it is time taking, there is nothing to be worried about that because in the solution section it will be identified and rectified. It will be observed that the error encounters during the execution process of a single Bash shell command. And why it gives an unsuccessful result as an unexpected token error. While fixing the single command error the way you can use it to add or remove an incorrect character that can cause the syntax error in the command. You can fix the error when you run a command having a syntax error. Take care of the mentioned error line by the Bash syntax. You can execute the error in the Bash shell to get fast access without rerunning and changing the script again and again. You need to update the command in the way correct to find that the script works properly. For instance, if you have a file on a Linux system where you want to rename it to report_jan.csv, you will write the code in this way;
mv report(july).csv report_jan.csv
if you run this script you will get the error message, but what is the reason that it gives you an error because the parenthesis () are used to create subshell in Bash, simply they are considered as special characters, and special characters need quotes.
-bash: syntax error near unexpected token `('
A special character can be escaped if treated as a normal character in command and to escape you need to use the backslash.
mv report(july).csv report_july.csv
No error will be shown now.
-rw-r--r-- 1 ec2-user ec2-user 28 Jun 28 22:29 report_july.csv
If you are writing Bash syntax script and you are missing between space that is considered as a character and ([), you will find the error message.
The correct way to write the script will be like this:
#!/bin/bash
DAY="Monday"
if [ $DAY == "Monday" ]; then
echo "Today is Monday"
else
echo "Today is not Monday"
fi
Writing a Bash script can cause an error at the beginning like this:
localhost)$ for i in {0..10} ; do echo $i ; then echo "Printing next number" ; done
This single line will give you the result as;
-bash: syntax error near unexpected token `then'
If you are using For Loop then the written syntax in Bash should be as under:
for VARIABLE in {0..10}
do
echo command1
echo command2
echo commandN
done
you can see here that the semicolon is used to separate the commands in Bash. Many other reasons are related to the codes that may cause Bash syntax error near unexpected token ‘newline’. Whenever you get this error means you are making a syntax error, first of all, read the file that contains the cat command.
cat file. sh
Note the use of special characters. The < character is used for input redirection, and redirection.
For this, if you are using the angle brackets “<>” must be used in single or double quotes, otherwise you will get the problem. For example, the following code will create an error syntax error near the unexpected token ‘newline’.
#!/bin/bash
str=
Using the quotes will give you the correct results like whether single or double quotes,
#!/bin/bash
str=""
#!/bin/bash
str=''
Another important point is that you can’t use angle brackets in Bash command line parameters without quotation signs. Let’s see the example;
If you have a login and password program containing an angle bracket will deliver the error syntax error near the unexpected token ‘newline’.
./login --username admin --password 1234<</code>
The argument should enclose in single or double quotes;
./login --username admin --password "1234<"</code>
The Conclusion #
The Bash syntax error near unexpected token ‘newline is an error that is invoked when messing with the Bash script that doesn’t use the syntax properly to execute the git add. The most important problem related to the syntax includes “<>” (angle brackets) it shouldn’t be used in both cases without single or double quotes, so wrapping up the angles brackets in quotes can stop this error. Another problem is the missing characters and spaces that can be avoided before you execute your program.
If while opening your bash you get an error message like «syntax error near unexpected token `newline'», this is because now your gentoo bash reads the .bashrc file originally from ubuntu. Check the line that is complaining by editing or commenting it, or create a new gentoo-based .bashrc file and you won’t see the message anymore. Below I copy-pasted a working .bashrc file for gentoo.
# /etc/bash/bashrc
#
# This file is sourced by all *interactive* bash shells on startup,
# including some apparently interactive shells such as scp and rcp
# that can't tolerate any output. So make sure this doesn't display
# anything or bad things will happen !
# Test for an interactive shell. There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if [[ $- != *i* ]] ; then
# Shell is non-interactive. Be done now!
return
fi
# Bash won't get SIGWINCH if another process is in the foreground.
# Enable checkwinsize so that bash will check the terminal size when
# it regains control. #65623
# http://cnswww.cns.cwru.edu/~chet/bash/FAQ (E11)
shopt -s checkwinsize
# Disable completion when the input buffer is empty. i.e. Hitting tab
# and waiting a long time for bash to expand all of $PATH.
shopt -s no_empty_cmd_completion
# Enable history appending instead of overwriting when exiting. #139609
shopt -s histappend
# Save each command to the history file as it's executed. #517342
# This does mean sessions get interleaved when reading later on, but this
# way the history is always up to date. History is not synced across live
# sessions though; that is what `history -n` does.
# Disabled by default due to concerns related to system recovery when $HOME
# is under duress, or lives somewhere flaky (like NFS). Constantly syncing
# the history will halt the shell prompt until it's finished.
#PROMPT_COMMAND='history -a'
# Change the window title of X terminals
case ${TERM} in
[aEkx]term*|rxvt*|gnome*|konsole*|interix)
PS1='[33]0;u@h:w07]'
;;
screen*)
PS1='[33ku@h:w33\]'
;;
*)
unset PS1
;;
esac
# Set colorful PS1 only on colorful terminals.
# dircolors --print-database uses its own built-in database
# instead of using /etc/DIR_COLORS. Try to use the external file
# first to take advantage of user additions.
use_color=false
if type -P dircolors >/dev/null ; then
# Enable colors for ls, etc. Prefer ~/.dir_colors #64489
LS_COLORS=
if [[ -f ~/.dir_colors ]] ; then
# If you have a custom file, chances are high that it's not the default.
used_default_dircolors="no"
eval "$(dircolors -b ~/.dir_colors)"
elif [[ -f /etc/DIR_COLORS ]] ; then
# People might have customized the system database.
used_default_dircolors="maybe"
eval "$(dircolors -b /etc/DIR_COLORS)"
else
used_default_dircolors="yes"
eval "$(dircolors -b)"
fi
if [[ -n ${LS_COLORS:+set} ]] ; then
use_color=true
fi
unset used_default_dircolors
else
# Some systems (e.g. BSD & embedded) don't typically come with
# dircolors so we need to hardcode some terminals in here.
case ${TERM} in
[aEkx]term*|rxvt*|gnome*|konsole*|screen|cons25|*color) use_color=true;;
esac
fi
if ${use_color} ; then
if [[ ${EUID} == 0 ]] ; then
PS1+='[33[01;31m]h[33[01;34m] W $[33[00m] '
else
PS1+='[33[01;32m]u@h[33[01;34m] w $[33[00m] '
fi
alias ls='ls --color=auto'
alias grep='grep --colour=auto'
alias egrep='egrep --colour=auto'
alias fgrep='fgrep --colour=auto'
else
if [[ ${EUID} == 0 ]] ; then
# show root@ when we don't have colors
PS1+='u@h W $ '
else
PS1+='u@h w $ '
fi
fi
for sh in /etc/bash/bashrc.d/* ; do
[[ -r ${sh} ]] && source "${sh}"
done
# Try to keep environment pollution down, EPA loves us.
unset use_color sh
I’m a complete BASH beginner who needs to make sure this script works. It’s supposed to restore an evenstore backup from an azure blob storage. Unfortunately it doesn’t work.
When I run it in ShellCheck:
Line 14:
export AZURE_STORAGE_KEY=<Insert key>
^-- SC1009 (info): The mentioned syntax error was in this simple command.
^-- SC1073 (error): Couldn't parse this variable assignment. Fix to allow more checks.
^-- SC1072 (error): Fix any mentioned problems and try again.
When I actually run it :
restore_eventstore.sh: line 14: syntax error near unexpected token `newline'
restore_eventstore.sh: line 14: `export AZURE_STORAGE_KEY=<Insert key>'
I have googled these errors but I was unable to find an answer for my case. Any help appreciated. Here is the script in its entirety:
#!/bin/bash
# Needs azure cli to work.
# Install command: curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
backup_name=$1
if [ -z "$backup_name" ]
then
echo "Must supply an file to use in the restore"
exit
fi
export AZURE_STORAGE_ACCOUNT=stoclbackups
export AZURE_STORAGE_KEY=<Insert key>
echo "Downloading backup $backup_name from blob storage"
az storage blob download --container-name node1backups --name $backup_name --file $backup_name
if [ ! -s "$backup_name" ]
then
echo "Failed to download the backup file $backup_name"
exit
fi
echo "Stopping the eventstore service"
systemctl stop eventstore
if [ -d eventstore_backup ]
then
echo "Removing old local eventstore backup"
rm -rf eventstore_backup
fi
echo "Doing an backup of the current db to eventstore_backup"
mkdir eventstore_backup
cp -r db/* eventstore_backup
echo "Removing the old data from eventstore db directory"
rm -rf db/*
echo "Unpacking the backup $backup_name into the eventstore db directory"
tar -zvxf $backup_name -C db/
echo "Truncate the eventstore data to the latest chaser checkpoint"
cp db/chaser.chk db/truncate.chk
echo "Restore is complete. Start the eventstore service with: sudo systemctl start eventstore"
Have you ever seen the message “syntax error near unexpected token” while running one of your Bash scripts?
In this guide I will show you why this error occurs and how to fix it.
Why the Bash unexpected token syntax error occurs?
As the error suggests this is a Bash syntax error, in other words it reports bad syntax somewhere in your script or command. There are many things that can go wrong in a Bash script and cause this error. Some common causes are missing spaces next to commands and lack of escaping for characters that have a special meaning for the Bash shell.
Finding the syntax error reported when you execute your script is not always easy. This process often requires you to change and retest your script multiple times.
To make your life easier I have analysed different scenarios in which this syntax error can occur. For every scenario I will show you the script or command with the error and the fix you need to apply to solve the problem.
Let’s get started!
One Approach to Fix Them All
Considering that this syntax error can occur in multiple scenarios you might not be able to find your exact error in the list below.
Don’t worry about it, what matters is for you to learn the right approach to identify what’s causing the error and knowing how to fix it.
And going through the examples below you will learn how to do that.
In some of the examples I will show you how to fix this error if it happens while executing a single command in a Bash shell.
In other examples we will look at Bash scripts that when executed fail with the “unexpected token” error.
To fix the error in a single command it’s usually enough to add or remove some incorrect characters that cause the syntax error in the command.
Knowing how to fix the error in a script can take a bit more time, and for that I will use the following 5-step process:
- Run the script that contains the syntax error.
- Take note of the line mentioned by the Bash error.
- Execute the line with the error in a Bash shell to find the error fast (without having to change the script and rerun it multiple times).
- Update your script with the correct line of code.
- Confirm the script works.
Makes sense?
It’s time for the first scenario.
Let’s say I have the following file on my Linux system:
-rw-r--r-- 1 ec2-user ec2-user 28 Jun 28 22:29 report(july).csv
And I want to rename it to report_july.csv.
I can use the following command, right?
mv report(july).csv report_july.csv
When I run it I get the following error:
-bash: syntax error near unexpected token `('
But, why?
Because parentheses () are used in Bash to create a subshell. In other words they are special characters.
And Bash special character need to be escaped if used as normal characters in a command. The backslah is used to escape characters.
I will update the command to include the backslash before both parentheses:
mv report(july).csv report_july.csv
No errors this time:
-rw-r--r-- 1 ec2-user ec2-user 28 Jun 28 22:29 report_july.csv
Lesson 1: Remember to escape Bash special characters when you use them as normal characters (literals) in a filename or string in general.
First error fixed!
Syntax Error Near Unexpected Token Then (Example 1)
And here is the second scenario.
When I run the following script:
#!/bin/bash
DAY="Monday"
if[ $DAY == "Monday" ]; then
echo "Today is Monday"
else
echo "Today is not Monday"
fi
I get back the error below:
(localhost)$ ./unexpected_token.sh
./unexpected_token.sh: line 5: syntax error near unexpected token `then'
./unexpected_token.sh: line 5: `if[ $DAY == "Monday" ]; then'
Can you see why?
The error is caused by the missing space between if and the open square bracket ( [ ).
And the reason is the following:
if is a shell builtin command and you might be thinking you are using if here. But in reality the shell sees if[ that is not a known command to the shell.
At that point the shell doesn’t know how to handle then given that it hasn’t found if before, and it stops the script with the error above.
The correct script is:
#!/bin/bash
DAY="Monday"
if [ $DAY == "Monday" ]; then
echo "Today is Monday"
else
echo "Today is not Monday"
fi
I have just added a space between if and [ so the shell can see the if command.
And the output of the script is correct:
(localhost)$ ./unexpected_token.sh
Today is Monday
Lesson 2: Spaces are important in Bash to help the shell identify every command.
Syntax Error Near Unexpected Token Then (Example 2)
While writing Bash scripts, especially at the beginning, it’s common to do errors like the one below:
(localhost)$ for i in {0..10} ; do echo $i ; then echo "Printing next number" ; done
When you run this one-liner here’s what you get:
-bash: syntax error near unexpected token `then'
Let’s find out why…
The syntax of a for loop in Bash is:
for VARIABLE in {0..10}
do
echo command1
echo command2
echo commandN
done
And using a single line:
for VARIABLE in {0..10}; do echo command1; echo command2; echo commandN; done
So, as you can see the semicolon is used in Bash to separate commands when you want to write them on a single line.
The reason why the semicolons were not required in the first version of the script is that the newline is a command separator too.
Now, let’s go back to our error…
The one-liner that was failing with an error contains the then statement that as you can see is not part of the structure of a for loop.
The error is telling us:
- There is a syntax error.
- The token ‘then‘ is unexpected.
Let’s confirm the one-liner runs well after removing then:
(localhost)$ for i in {0..10} ; do echo $i ; echo "Printing next number" ; done
0
Printing next number
1
Printing next number
2
Printing next number
3
Printing next number
4
Printing next number
5
Printing next number
6
Printing next number
7
Printing next number
8
Printing next number
9
Printing next number
10
Printing next number
All good!
Lesson 3: When you see a syntax error verify that you are using Bash loops or conditional constructs in the right way and you are not adding any statements that shouldn’t be there.
Syntax Error Near Unexpected Token Done
I have created a simple script in which an if statement is nested inside a while loop. It’s a very common thing to do in Bash.
#!/bin/bash
COUNTER=0
while true
do
if [ $COUNTER -eq 0 ]; then
echo "Stopping the script..."
exit 1
done
fi
This script might seem ok, but when I run it I get the following…
./unexpected_token.sh: line 8: syntax error near unexpected token `done'
./unexpected_token.sh: line 8: ` done'
Why?
The done and fi statements are correctly used to close the while loop and the if conditional statement. But they are used in the wrong order!
The if statement is nested into the while loop so we should be closing the if statement first, using fi. And after that we can close the while loop using done.
Let’s try the script:
(localhost)$ ./unexpected_token.sh
Stopping the script...
All good now.
Lesson 4: Nested loops and conditional statements need to be closed in the same order in which they are opened.
Syntax Error Near Unexpected Token fi
Let’s look at another scenario in which this syntax error can occur with the fi token:
#!/bin/bash
for NAME in 'John' 'Mark' 'Kate'
do
if [ "$NAME" == 'Mark' ] then
echo 'Hello Mark!'
fi
done
And this is what I get when I run it:
./unexpected_token.sh: line 7: syntax error near unexpected token `fi'
./unexpected_token.sh: line 7: ` fi'
In this case the Bash shell identifies the if statement and because of that it expects then after it.
As you can see then is there, so what’s the problem?
There is no command separator between the [ ] command (yes….it’s a command) and the then statement.
So, what’s the fix?
Add a command separator immediately after the closing square bracket. We will use the semicolon ( ; ) as command separator.
Our script becomes:
#!/bin/bash
for NAME in 'John' 'Mark' 'Kate'
do
if [ "$NAME" == 'Mark' ]; then
echo 'Hello Mark!'
fi
done
And if I run it I get the correct output:
(localhost)$ ./unexpected_token.sh
Hello Mark!
Lesson 5: Remember to specify command separators in your Bash scripts. Either the semicolon or the newline.
Conclusion
You now have what you need to understand what causes this syntax error in your scripts. You can apply the 5 lessons I have explained in this guide to find a fix.
Take the time to review the lessons at the end of each section so they become part of your Bash knowledge.
If you have any questions please feel free to write them in the comments below.
Now, let’s say you have saved your Bash script using Windows.
And when you run it in Linux you are seeing a syntax error that you can’t really explain because the script looks correct to you.
You might be having the problem explained in this article.
Enjoy your scripting!
Related FREE Course: Decipher Bash Scripting
Related posts:

I’m a Tech Lead, Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!