I am a newbie in bash script.
Here is my environment:
Mac OS X Catalina
/bin/bash
I found here a mix of several commands to remove the duplicate string in a string.
I needed for my program which updates the .zhrc profile file.
Here is my code:
#!/bin/bash
a='export PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:"'
myvariable=$(echo "$a" | tr ':' '\n' | sort | uniq | xargs)
echo "myvariable : $myvariable"
Here is the output:
xargs: unterminated quote
myvariable :
After some test, I know that the source of the issue is due to some quotes "" inside my variable '$a'.
Why am I so sure?
Because when I execute this code for example:
#!/bin/bash
a="/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home:/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home"
myvariable=$(echo "$a" | tr ':' '\n' | sort | uniq | xargs)
echo "myvariable : $myvariable"
where $a doesn't contain any quotes, I get the correct output:
myvariable : /Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home
I tried to search for a solution for "xargs: unterminated quote" but each answer found on the web is for a particular case which doesn't correspond to my problem.
As I am a newbie and this line command is using several complex commands, I was wondering if anyone know the magic trick to make it work.
aat the colons into several lines. Therefore one of the strings starts withexportand ends withHome, and has a quote somewhere in the middle. Another string ends with a quote. Then you sort them, and then glue this pieces together. It is not clear that the parts will end up in a way that the result is a well-formed command again, where the quotes are in the right place.(echo '"a'; echo 'b"')|xargs, which results in an unmatched double quote error message.sorting is that you'll likely change the order of the entries which in turn could have undesired consequences (eg,$PATHwas originally built with a specific precedence/directory-ordering in mind, but that precedence/ordering has been mangled due to thesorting); net result is that for variables like$PATHyou'll probably want to look at solutions that maintain the order of the individual directories