0

The primary goal is to know how to use the sed command on a array-variable rather then a file.

Secondary is to remove the path out of the array, the final output should contain only the files names

sample data:

path=$(pwd) 
echo $path
>> /home/peter/my_commands

array=($path/*.sh)
echo "${array[@]}"
>> /home/peter/my_commands/func1.sh /home/peter/my_commands/main.sh /home/peter/my_commands/test.sh

desired output: func1.sh main.sh test.sh this values inside of array-variable called array2

summarizing:

#input data
array=(/home/peter/my_commands/func1.sh /home/peter/my_commands/main.sh /home/peter/my_commands/test.sh)

#sed command to make the transformation
#HERE..

#output will be
array2=(func1.sh  main.sh  test.sh)

failed attempts:

# sed -i -e 's.$path..'
# array2=$(sed -n '2p' $path)
# array2=(($path/*.sh) | sed -i "s.$path..")
# array2="$(echo $array | sed -i -e "s.$path..g")"
# array2=$(sed -i -e "s./.." <<< "$array")

echo "${array2[@]}"

1
  • let me edit the question @GillesQuenot Commented Dec 1, 2022 at 19:07

1 Answer 1

2

Like this:

$ array=(/home/peter/my_commands/func1.sh /home/peter/my_commands/main.sh /home/peter/my_commands/test.sh)
$ array2=( "${array[@]##*/}" )
$ printf '%s\n' "${array2[@]}"
func1.sh
main.sh
test.sh

Using bash parameter expansion

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.