1

I have two variables in a bash script

hostname="ab78ascsoadp003.abc.com"
Loc=`$hostname | cut -c3,4`

I am getting an error ab78ascsoadp003.abc.com: command not found

I am trying to use cut command so that $Loc gets 78

oguz ismail
  • 34,491
  • 11
  • 33
  • 56
meallhour
  • 9,007
  • 13
  • 36
  • 72

3 Answers3

3

While you can use cut to achieve this, sometimes it is useful to stick to :

hostname="ab78ascsoadp003.abc.com"
Loc=${hostname:3:2}

${parameter:offset:length} Substring Expansion. Expands to up to length characters of parameter starting at the character specified by offset. If length is omitted, expands to the substring of parameter starting at the character specified by offset. length and offset are arithmetic expressions <snip>

source: man bash

kvantour
  • 20,742
  • 4
  • 38
  • 51
2
hostname="ab78ascsoadp003.abc.com"
Loc=$(cut -c3,4 <<<"$hostname")
oguz ismail
  • 34,491
  • 11
  • 33
  • 56
1

you are missing an echo

Loc=`echo $hostname | cut -c3,4`
Eric
  • 489
  • 2
  • 7