Reputation: 1
Good day,
I have a simple UNIX script test.sh
I need to substitute the value of a variable. This variable contains a directory path.
My test.sh script
#!/bin/sh
filepath="/host/messages/in/documents"
archivePath=`${filepath/\/in/\/archive/}`
echo "archive path is " $archivePath
I get a "bad substitution" error when I run it.
The required output for archivePath should be:
/host/messages/archive/documents
What am I doing wrong and what could be a possible solution?
Upvotes: 0
Views: 156
Reputation: 1
found a solution with sed:
#!/bin/sh
filepath="/host/messages/in/documents"
echo $filepath | sed -e %s/in/archive/g
archivePath=`echo $filepath | sed -e s/in/archive/g`
echo "archive path is $archivePath"
Upvotes: 0
Reputation: 367
Must use bash (or ksh or zsh), use correct syntax ${varname/pattern/replacement}
and escape /
by \
in pattern
and replacement
.
#!/bin/bash
filepath="/host/messages/in/documents"
archivePath="${filepath/\/in\//\/archive\/}"
echo "archive path is $archivePath"
Upvotes: 1