I try to generate a random string using this command:

tr -dc 'A-Za-z0-9!#$%&()*+,-./:;<=>?@[\]^_`{|}~' </dev/urandom | head -c16; echo; 

I need to put that command into a function within a shell script which needs to run on AIX and Linux. As bash is not installed on our AIX machines I need to use ksh93. When I try to execute this command in bash (or zsh) it works as expected. But in KSH it fails with 'invalid source string'.

I tried to rearrange the string and deleted some of the characters but without success. The output is

tr: A-Za-z0-9!#$%&()*+,-./:;<=>?@[\]^_`{|}~: invalid source string

How do I need to pass this string to ksh to have that working?

Thanks in advance

  • You're might be running into problems with ,-. or [\] – glenn jackman Jan 3 '18 at 13:33
  • found the solution based on the input of @glennjackman. Thanks for your fast response. – user266964 Jan 3 '18 at 13:46
  • 1
    To make this a useful question for others, move your EDIT text into an answer, then accept the answer. – glenn jackman Jan 3 '18 at 16:00
  • "Bash is not installed", but "when you execute it in bash, it works as expected"? Perhaps you mean that regardless of the shell, it works on Linux, but not on AIX? The fact that the error message comes from tr also points at that. – ilkkachu Jan 3 '18 at 20:14
  • No. I write the script on Linux having ksh and bash. In ksh the error occurred but not in bash. As the same string works with bash it must be a difference between how bash and ksh pass the source string to tr. – user266964 Jan 3 '18 at 20:48

Thanks for the input @glenn-jackman:

When I remove


from the list I got the error obout

invalid multibyte character byte

but I already knew that LC_ALL must be set to avoid that. I escaped the string now. The working snippet is

LC_ALL=C tr -dc 'A-Za-z0-9!#$%&()*+,\-./:;<=>?@[\\]^_`{|}~' < /dev/urandom | head -c16; echo;


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy