I am trying to assign the output of hostname to a variable and use it in my script. While I was able to get the variable to another logic, when I try to print the variable it doesn't print the variable.

Here is what I am looking to do:

the hostname on the server returns xy29abcd01.

I want to assign this to a variable and then use it to remove 3rd and 4th characters of the hostname and assign it to another variable.

Here is what I have


print " Get queue manager name"
SERVER="echo `hostname`"
export QMGR=`$SERVER |cut -c1-2,5-10| tr [[:lower:]] [[:upper:]`

print "Queue manager name $QMGR"

And here is the output When I run this:

Get queue manager name
./test.sh[13]: printecho xy29abcd01: not found [No such file or directory]
Queue manager name XYABCD01

What am I doing wrong here to assigning the SERVER variable. Desired output it

Queue manager name XYABCD01
  • use www.shellcheck.net to check your syntax first
    – Rahul
    Jul 11, 2016 at 6:50

2 Answers 2


Your script has a few issues:

  1. The error stems from the export line. You are using $SERVER as a command, and this command can not be found. What you'd like to do is to feed the string into cut:

    print $SERVER | cut -c1-2,5-10 | ...
  2. The line SERVER="echo `hostname`" is better written

  3. The tr utility takes character ranges, so there's no need to enclose [:upper:] or [:lower:] in [ ... ]. Do, however, enclose them in quotes:

    tr '[:lower:]' '[:upper:]'

    It's a good habit for the day when you want to use tr for things like * that the shell will be treating as a globbing pattern otherwise.

  4. In general, do use $( ... ) rather than backticks. It makes the code more readable, they nest easier, and is better in many other ways.


Create a bash script.

echo "${OUTPUT}"

Run the bash script using ./scriptname.sh

You must log in to answer this question.

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