Score:-1

expr: syntax error: unexpected argument ‘Desktop’

Here is my bash script for converting the input temperature from Celsius to Fahrenheit.

#!/bin/bash
echo "Enter temperature in degrees Celsius: "
read celsius
temp=$(expr $celsius * 9 / 5 + 32 )
echo "Temperature in degrees Fahrenheit: $temp"

I got this error message:

expr: syntax error: unexpected argument ‘Desktop’

Do you have any solutions?

Someone avatar
my flag
What is your Ubuntu version ? Please can you [edit] and add some info
Someone avatar
my flag
This question has severe formatting or content problems. This question is unlikely to be salvageable through editing, and might need to be removed
karel avatar
sa flag
The text of the bash script in the question was copied into my answer to this question.
Score:2
hr flag

The * symbol is a shell glob (filename generation) character it will expand to a list of the non-hidden files in the current directory - in your case, that appears to include a Desktop item.

To prevent that, you either need to turn off shell globbing, using set -f or set -o noglob:

#!/bin/bash
set -f
echo "Enter temperature in degrees Celsius: "
read celsius
temp=$(expr "$celsius" * 9 / 5 + 32 )
echo "Temperature in degrees Fahrenheit: $temp"

or either quote or escape the * character to make it literal:

#!/bin/bash
echo "Enter temperature in degrees Celsius: "
read celsius
temp=$(expr "$celsius" \* 9 / 5 + 32 )
echo "Temperature in degrees Fahrenheit: $temp"

In either case you should get into the habit of double-quoting shell expansions like $celsius, to prevent shell expansion and word-splitting (especially when they contain arbitrary user input).

Score:1
sa flag

Corrected bash script for converting the input temperature from Celsius to Fahrenheit using the popular bc terminal program instead of expr in line 4:

#!/bin/bash
echo "Enter temperature in degrees Celsius: "
read celsius
temp=`echo "scale=1; $celsius*1.8 + 32" | bc` 
echo "Temperature in degrees Fahrenheit: $temp"

bc for "basic calculator" is an arbitrary precision calculator language. bc is a lot more intuitive and easier to use than working clumsily and fumbling with an expr syntax error. The value of the scale function is the number of digits after the decimal point in the expression.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.