I have a variable in my shell script that works as per my expectations. But when I use that variable as filename, I get ?? at the beginning of the string. How do I trim the junk characters?
I use $(some command) to create this variable.
I have a variable in my shell script that works as per my expectations. But when I use that variable as filename, I get ?? at the beginning of the string. How do I trim the junk characters?
I use $(some command) to create this variable.
There are any number of ways to fix your variable but first we need to know what to get rid of (i.e., the output of someCommand
).
If, for example, it's always putting two characters at the front that you don't need, you could use:
x=$(someCommand | cut -c3-)
If you want to strip off all characters in the output outside the range 0x20 through 0x7e (the non-printable characters from ASCII
), you could use:
x=$(someCommand | tr -d '[\000-\037\177]')
Ideally, you would run someCommand | od -xcb
to get an actual hex dump of what it's outputting. Then we can help you out further.
Update:
Based on your new data (that the output is \n\nSomeName27-Oct-2009-14-33.sql.zip
), you should be able to use sed '/^$/d'
to strip out the empty lines.
pax> echo '
SomeName27-Oct-2009-14-33.sql.zip' | od -xcb
0000000 0a0a 6f53 656d 614e 656d 3732 4f2d 7463
\n \n S o m e N a m e 2 7 - O c t
012 012 123 157 155 145 116 141 155 145 062 067 055 117 143 164
0000020 322d 3030 2d39 3431 332d 2e33 7173 2e6c
- 2 0 0 9 - 1 4 - 3 3 . s q l .
055 062 060 060 071 055 061 064 055 063 063 056 163 161 154 056
0000040 697a 0a70
z i p \n
172 151 160 012
0000044
pax> echo '
SomeName27-Oct-2009-14-33.sql.zip' | sed '/^$/d' | od -xcb
0000000 6f53 656d 614e 656d 3732 4f2d 7463 322d
S o m e N a m e 2 7 - O c t - 2
123 157 155 145 116 141 155 145 062 067 055 117 143 164 055 062
0000020 3030 2d39 3431 332d 2e33 7173 2e6c 697a
0 0 9 - 1 4 - 3 3 . s q l . z i
060 060 071 055 061 064 055 063 063 056 163 161 154 056 172 151
0000040 0a70
p \n
160 012
0000042
That's assuming those \n
markers are actually two newlines ({'\n','\n'}
). If (for some bizarre reason) they're the four characters {'\\','n','\\','n'}
, use cut -c5-
instead.
A simple way I would suggest is to use a tr command with -d option to delete the ?
filename=$filename | tr -d '?'