Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

I just faced an issue when I tried to start a bash script which actually updates itself if there is a newer script version available and then calls itself again - but now the updated version with the same invocation parameters. This script update should happen transparently for the script user so all the time the script is invoked the latest script version is used.

 

There are string parameters passed which include spaces but the updated script doesn't get the parameters the same way as when it was called initially when I just pass $@. But it works very nice if you save the invocation parameters in an array and pass the array contents as a parameter in the script call of the updated script.

parameters=()
for (( i=1; i<=$#; i++ )); do
    parameters+=("${!i}")
done

and call the updated script with

exec "$(which bash)" --noprofile "$0" "${parameters[@]}"   

A proof of concept bash code can be downloaded here

 

Update 19.12.2020: Manul kindly mentioned in a comment on the German page it's much easier to pass parameters with spaces on: Just use "$@" and you're fine.

 

Add comment

*** Note ***

Comments are welcome. But in order to reject spam posts please consider following rules:
  1. Comments with string http are rejected with message You have no rights to use this tag
  2. All comments are reviewed by hand and thus it usually takes one day until a comment will be published.