Sponsored Content
Top Forums UNIX for Advanced & Expert Users Without the aid of the escape mechanism, how to put a raw string into a variable? Post 303045761 by wisecracker on Sunday 12th of April 2020 11:11:40 AM
Old 04-12-2020
Attached to previous and don't know why.

Quote:
Originally Posted by MadeInGermany
An embedded string in a shell script is normally fenced with either ' or " so you need to escape these characters inside the string.
Try a here doc:
Code:
read STRING <<"EOT"
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
EOT

Now the fence is a custom word.
You must quote it to prevent variable substitutions.
BTW if a file or a here doc has got one line then read is smarter than cat.
Code:
Last login: Sun Apr 12 14:58:50 on ttys000
AMIGA:amiga~> read STRING <<"EOT"
>  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
> EOT
AMIGA:amiga~> 
AMIGA:amiga~> echo "${STRING}"
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~
AMIGA:amiga~> 
AMIGA:amiga~> # The space is missing!
AMIGA:amiga~> 
AMIGA:amiga~> # Also the '\' too.
AMIGA:amiga~> 
AMIGA:amiga~> echo "${#STRING}"
93
AMIGA:amiga~> # Should be 95.
AMIGA:amiga~> 
AMIGA:amiga~> read -r STRING <<"EOT"
>  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
> EOT
AMIGA:amiga~> echo "${STRING}"
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
AMIGA:amiga~> 
AMIGA:amiga~> # The space is still missing.
AMIGA:amiga~> 
AMIGA:amiga~> echo "${#STRING}"
94
AMIGA:amiga~> _

cat does not omit the space, (decimal character 32).

--- Post updated at 05:11 PM ---

Hi Rudic...

(HOPEFULLY THIS DOES NOT ATTACH TO THE PREVIOUS, if it does apologies...)
Code:
Last login: Sun Apr 12 15:58:43 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> ./rawstr.sh

Raw string:-
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
Number of characters:-
98

Raw string:-
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
Number of characters:-
95

Look at the real string inside the environment:-

Apple_PubSub_Socket_Render='/private/tmp/com.apple.launchd.glJfvo3uxT/Render'
HOME='/Users/amiga'
IFS='     
'
LANG='en_GB.UTF-8'
LINENO=''
LOGNAME='amiga'
OLDPWD='/Users/amiga'
OPTIND='1'
PATH='/Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Users/amiga/Programs:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
PPID='4629'
PS1='AMIGA:\u\w> '
PS2='> '
PS4='+ '
PWD='/Users/amiga/Desktop/Code/Shell'
SHELL='/bin/bash'
SHLVL='1'
SSH_AUTH_SOCK='/private/tmp/com.apple.launchd.gtetHEIugk/Listeners'
STRING=' !"#$%&'"'"'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
TERM='xterm-256color'
TERM_PROGRAM='Apple_Terminal'
TERM_PROGRAM_VERSION='421.2'
TERM_SESSION_ID='8D9826C6-8625-4C9F-A6C9-E7CBDDEAC0CE'
TMPDIR='/var/folders/9c/bb_c667j5nj5cbbkw__1l8n40000gp/T/'
USER='amiga'
XPC_FLAGS='0x0'
XPC_SERVICE_NAME='0'
_='./rawstr.sh'
AMIGA:amiga~/Desktop/Code/Shell> _

Thanked your previous post...
Code:
#!/usr/local/bin/dash
# rawstr.sh

STRING=' !"#$%&\047()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'

# Assumption that this is using dash's builtin 'echo'...
echo ""
echo "Raw string:-"
echo "${STRING}"
echo "Number of characters:-"
echo "${#STRING}"

STRING=$( echo "${STRING}" )

echo ""
echo "Raw string:-"
echo "${STRING}"
echo "Number of characters:-"
echo "${#STRING}"
echo ""
echo "Look at the real string inside the environment:-"
echo ""

set

# YES! (Fist in the air!)
# Nice one RudiC, I now have an idea for a convoluted means
# editing a raw ASCII string variable in POSIX mode and will
# upload IF it works at a later date...

OSX 10.14.x, default bash terminal, calling dash inside the code...

Last edited by wisecracker; 04-12-2020 at 12:21 PM..
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Put raw data to column data

Dear all, I want below data to make it in column format.so i will see the data like this cdrID teleServiceCode chargedPartyNumber ... ... ... ... "egmailcom0w10ggzx00" 'sMS (5)' "716323770" "m17ifi5z30w0z6o7200" 'sMS (5)' ... (7 Replies)
Discussion started by: Nayanajith
7 Replies

2. UNIX for Advanced & Expert Users

Remove escape characters from string

Hello all, I have a string var which contains formatting characters at the end, it is a string with EScape sequences at the end of it. How can I remove them so that I only keep the 'real' text? I tried : var1=${var1%%\033[0m} does not seem to do the job .... Please help Thanks (2 Replies)
Discussion started by: gio001
2 Replies

3. Programming

how to use hex escape char with string in C?

I want it to ouput "abcd", but it dosen't. 1 #include<stdio.h> 2 int main() 3 { 4 printf("a\x62cd"); 5 } 6 gcc alarm.c -o alarm alarm.c: In function 'main': alarm.c:4:9: warning: hex escape sequence out of range It seems that the complier joint "cd" as part of... (8 Replies)
Discussion started by: vistastar
8 Replies

4. Shell Programming and Scripting

awk not escape my bash variable

I tried to parse data from switch configuration files vlan 1727 name SQ5506-15 by port tagged ethe 8/1 to 8/2 untagged ethe 1/13 ! vlan 2105 name SQ5620-7007(BR2) by port tagged ethe 8/1 to 8/2 untagged ethe 1/17 ! interface ethernet 1/13 port-name SQ5506-15.nic0 rate-limit... (2 Replies)
Discussion started by: winggundamth
2 Replies

5. Shell Programming and Scripting

How to store a escape character in a Variable.?

How to store escape character in the variable. Var=abc,def,ghi,jkl echo ${Var} | sed -e "s/,/|\\\\./g;s/^/\\\\./g" \.abc|\.def|\.ghi|\.hjk Var1=`echo ${Var} | sed -e "s/,/|\\\./g;s/^/\\\./g"` Actual: ------- echo $Var1 .abc|.def|.ghi|.jkl Expected: --------- echo $Var1... (4 Replies)
Discussion started by: deepakwins
4 Replies

6. UNIX for Beginners Questions & Answers

String has * as the field delimiter and I need echo/awk to escape it, how?

Hi, I am trying to read an Oracle listener log file line by line and need to separate the lines into several fields. The field delimiter for the line happens to be an asterisk. I have the script below to start with but when running it, the echo command is globbing it to include other... (13 Replies)
Discussion started by: newbie_01
13 Replies

7. Shell Programming and Scripting

How to escape colon sign from variable in shell?

Hello, Below script works fine when I manually enter required information for each file. When it comes to shell in auto mode, it gives various errors. I am under ubuntu 14.04 / trusty. manual_run.sh: #!/bin/bash /usr/bin/ffmpeg -start_at_zero -copyts -i nicki.mp4 -c:v mpeg2video \ -b:v 500k... (3 Replies)
Discussion started by: baris35
3 Replies

8. Shell Programming and Scripting

Escape characters in a variable

Debian 9 64x - LXDE How can i disable escape sequences in a variable? #!/bin/bash #mainscript . "./links.bash" echo "$red_start This text should be red $color_end"#!/bin/bash #links.bash #colors red_start="\eOutput that i get: \e Output expected: This text should be... (5 Replies)
Discussion started by: int3g3r
5 Replies
All times are GMT -4. The time now is 10:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy