Syntax error near unexpected token newline linux

Ubuntu bash presenting error «syntax error near unexpected token `newline'» after editing .bashrc file

I ran the command npm completion >> ~/.bashrc in the terminal, which made changes to my .bashrc file; now whenever the terminal is opened I receive the error:

bash: /home/user/.bashrc: line 126: syntax error near unexpected token `newline' bash: /home/user/.bashrc: line 126: `Usage: npm ' 
126: Usage: npm 127: 128: where is one of: 129: access, add-user, adduser. 
  1. reverse the npm completion >> ~/.bashrc changes with a terminal command (if so what is this command); or
  2. what do I change in the .bashrc file in order to remove the warnings from my terminal?

I suspect the simplest way for you to fix this would be to run nano ~/.bashrc to edit the file to remove the lines at the end of the file yourself. Other editors are obviously available, but nano is probably the simplest to use for someone unused to the way they work.

2 Answers 2

The npm command does not understand the completion argument and issues a help message to that effect.

The help message was then appended to your .bashrc :

When the shell interprets that line, it effectively wants to run

(The command «Usage:», with first arg «npm», stdio redirected from a file called «command» and output redirected to . oops, no file name after > , so syntax error.

The fix is to run the proper npm command that does’t produce a help message.

Isn’t it kind of weird that npm outputs its Usage-blurb on stdout, rather than stderr? If it had, chris_p_ wouldn’t have had syntax error in his .bashrc.

  1. You can’t easily do that; it seems you have run various commands appending lines to your .bashrc, one of them npm with an option it did not understand.
  2. Start your favourite text editor and remove the offending lines («Usage. » etc.) from the end if your .bashrc.

I removed everything below line 126 and the error was gone, and the npm help command still worked so it looks like that appendage was unnecessary. Thanks

npm help would always work; the purpose of running npm completion would be to generate the bash code necessary to allow tab completion of arguments to the npm command. However, your version of npm does not appear to support the completion command; it outputs an error message instead of lines intended to be added to .bashrc .

Читайте также:  Получить dns по ip linux

Adding stuff to .bashrc does not make «npm help» work/not work. The idea of the command you tried to run was to make «npm startofcommandTAB» do «completion» of the command. So for instance, if you wrote «npm insTAB», then bash would be able to complete that into «npm install » for you, saving some typing.

Источник

-bash: syntax error near unexpected token `newline’ for display command

I want to open a gif file using linux command.The file’s name is «Simulation of electrical activity in the heart during fibrillation (with comparison with experiment).gif». I use the following command:

-bash: syntax error near unexpected token `newline' 

Remove the < and >if those were in the command, and the \ should be before the ( and ) , not after. For convenience, use tab completion which automatically escapes the filename: display Simu .

«unable to open X server» means, well, just that. You don’t have a correct DISPLAY environment variable telling it how to contact your graphical display, or the Xauthority or other supporting content is wrong. There are several possible reasons your X server could be inaccessible, and the question doesn’t provide enough detail to focus on any one of them as more likely than any other.

. moreover, just because you have a different problem after fixing the question you asked about doesn’t mean that that first question was not, in fact, correctly solved.

1 Answer 1

gives you a syntax error near unexpected token newline is that the arrow brackets in whatever documentation you were following were not intended to be taken literally.

A < indicates that the thing that follows is a filename to be used as input on stdin, and a >indicates that the thing that follows is a filename to be used for output on stdout; thus, a > at the very end of your command is a syntax error, because it expects a file name to follow, not a newline.

display 'Simulation of electrical activity in the heart during fibrillation (with comparison with experiment).gif' 

Источник

How to fix the “syntax error near unexpected token `newline’” error

Oftentimes when we are using Bash scripting or the git add command, we can run into this error “syntax error near unexpected token `newline’”. What exactly does this code mean? Why does this code occur and what measures can we take to avoid this error? In the following article, we will answer all those queries for you.

Resolve the “syntax error near unexpected token `newline’” problem

The main reason that this error is invoked is that we do not follow the correct syntax in “.sh” file or with the use of git add. This section enlists all the reasons for the error “syntax error near unexpected token” and their respective solutions.

Reason 1: Bash Scripting

Make sure you are using the correct notations to initialize strings inside the bash file. To view the code of your bash script, you can use the following command:

Where the denotes a “.sh” file.

In our case, we have used the following command to get its content:

Читайте также:  Linux load cpu 100

As you can see that we are trying to use the “< >” (angle-brackets) while initializing a variable. We have executed the script to see how it behaves in the snippets below:

Solution

Every language has its notations and keywords that must be followed to avoid any execution/compilation error. The same goes for the “< >” (angle-brackets) signs in bash scripting. The “< >” (starting and ending angle brackets) together are used as placeholders in bash scripting and should not be used while writing strings.

Use of “< >” in strings

If you want to make use of “< >” in the strings, you can use them alongside quotation marks. A sample is shown below:

Let’s demonstrate how you can use these “< >” in bash scripting. Check out the correct syntax in the snippet below:

Now that we have placed quotations around these “< >” (angle brackets) as can be seen in the above image, we can execute the bash script without the error:

Reason 2: “git add” command

A similar error occurs while using the git add command. If you use the “< >” (angle-brackets) with the git add command this error will be invoked as shown below:

Solution: Remove Brackets

To fix this issue we simply need to remove the “< >” (angle-brackets) from the command and the error will be fixed as shown in the following snippet:

Conclusion

The “syntax error near unexpected token ‘newline’” error is invoked when we mess up the bash scripting syntax does not use the proper syntax to execute the git add command. The main problem with the syntax involves the “< >” bracket as it should not be used in both cases. You can wrap quotations around the “< >” (angle-brackets) to avoid this error in bash scripts as well as in git add. The way to fix it is to wrap the brackets with quotations so that it is read as a string. We have explained all these reasons and shown the practical implementation of solutions.

TUTORIALS ON LINUX, PROGRAMMING & TECHNOLOGY

Источник

How to Resolve Bash Terminal Error: “Bash: Syntax Error Near Unexpected Token ‘Newline’

When working with the Bash script, you can encounter the unexpected token “newline” error which affects the execution of your script. Understanding this error is the first step to solving it. This post discusses everything about this error. We will understand what triggers it and how you should resolve it to avoid reencountering it. Let’s begin!

What Is the Bash: Syntax Error Near Unexpected Token “Newline”

When executing a Bash script, you may run into the unexpected token “newline” error due to different reasons. This error occurs when Bash detects unexpected characters in your script or input.

If you are facing this error, a syntax error in your script is causing it. We will give different instances where the error can occur and see how to resolve it to fix the error. Take a look at the following examples.

Читайте также:  Kali linux window manager

Example 1: Unquoted Angle Brackets

Quote any angle brackets that you use within your code when writing a Bash script. Only then will Bash read it as an angle bracket. Otherwise, it raises the unexpected token “newline” error.

In the following image, we tried to execute a Bash script on the terminal and got the syntax error as follows:

The first thing to do when encountering such an error is to open your script using a text editor. We use the nano editor for this case.

When you open the script, look for any code which contains unquoted angle brackets. That’s the main cause of the error. In the previous image, we can see that our str variable has an unquoted string, and that’s the cause of the syntax error. You can fix this error by quoting it as shown in the following:

If we quote it as expected by Bash and rerun the same script, we will manage to run the script successfully.

Example 2: Using the Angle Brackets in the Bash Command Line Parameters

When you have a Bash script that takes an input from the user to use as arguments in a parameter in the script, adding the angle brackets without quoting them raises the unexpected token “newline” error.

If you want to include an argument that contains the angle brackets, make sure that you quote it. If we open the Bash script, we can check the syntax to identify the origin of the error.

In the previous script code, we can see that we are accepting the arguments from the user input. Thus, when we run the script, we must add the arguments as we did, causing the syntax error to appear. In the user input, we notice that we entered an angle bracket in the second argument which causes Bash to raise the syntax error.

However, if we quote the angle bracket and enter it again, we successfully run the Bash script without errors.

In the output, after quoting the angle brackets, we managed to print them on the terminal as per the script.

So, if you get the “Bash: syntax error near unexpected token “newline’” error, start by checking the code in your script to determine where the error is. Moreover, check if you have parameters in your code and if the error occurs due to an error in the argument that you enter on your terminal.

Conclusion

You can encounter the “Bash: syntax error unexpected token “newline”” when working with the Bash script due to two main reasons: you may not have quoted the angle brackets. Still, you could have an error in your argument. This post detailed these two causes and how to fix the error.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.

Источник

Оцените статью
Adblock
detector