construct a string with X number of spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting construct a string with X number of spaces
# 1  
Old 06-21-2010
construct a string with X number of spaces

I'd like to create a variable with the value of X number of space( no Perl please), printf seems to work, but , in following example,10 spaces becomes 1 space when assinged to a variable, Why? other solutions are welcome.


Code:
 
$printf "=%10s=\n"
=          =
$var=$(printf "=%10s=\n")
echo $var
= =

# 2  
Old 06-21-2010
in ksh you can use typeset -L (or -R) to define space padded strings
Code:
$ typeset -R10 x=
$ echo "=$x="    
=          =
$ echo =$x=    
= =

when you are passing a value with embedded spaces you always need to double quote the variable, otherwise echo (or any other function/utility) would treat multiple consecutive spaces as a single argument separator.
# 3  
Old 06-22-2010
export the value of IFS as below and try ..

Code:
$ export IFS=""
$ x=`printf "=%10s="`
$ echo $x
=          =
$


Last edited by Scott; 06-22-2010 at 02:04 AM.. Reason: Code tags, please...
# 4  
Old 06-22-2010
$var does contain the spaces but they disappear when you echo the variable. Try this:
Code:
echo "$var"

# 5  
Old 06-22-2010
try with printf Smilie
Code:
printf %s"\n" "$var"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Construct Array from String seperated by different whitespaces

My string variable which gets the output from the result of a database query has values as below: line="2019-09-11 15:17:55 CR1234 anonymous Deployed DR_only Back_APP" I wish to construct an array (my_array) which should have entries as below. Note: 1. The first... (6 Replies)
Discussion started by: mohtashims
6 Replies

2. Shell Programming and Scripting

Search for an undetermined number of spaces

I would like to find an undetermined number of spaces and shorten them to one space. I am running Debian in mksh. Script running in #!/bin/sh. Sorry to not include all code. The program is too big and involves an online file... too much hassle to solve a simple issue. Ex., I start with... (11 Replies)
Discussion started by: bedtime
11 Replies

3. Shell Programming and Scripting

Printing a specific number of spaces

when given a file name, im looking for the most efficient way to turn each letter of the file name into spaces. for instance, the way im thinking of going about this is this: MYFILE=check_disks.sh CHANUM=$(echo ${MYFILE} | awk '{ print length }') printf '%s\n' $CHANUM as you can see... (4 Replies)
Discussion started by: SkySmart
4 Replies

4. Shell Programming and Scripting

Print a specific number of spaces

i'm looking for a command that allows me to print a specific number of spaces which i will provide. something as simple as: spaces=4 printf " " * $spaces i'm looking for somethign that'll allow me to print a specific amount of spaces on a line. can awk be used for this? (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

determine the number of spaces

Here is a weird question :) i am trying to create a script written in bash that will create configuration files for nagios. As some of you aware is has to be written in the below format: define service{ option1 value1 option2 value2... (6 Replies)
Discussion started by: ppolianidis
6 Replies

6. UNIX for Dummies Questions & Answers

grep any number of spaces

which one of the following is the correct expression to ignore line with multiple spaces after any string cat file | grep -v "xyz *$" or cat file | grep -v "xyz*$" do i need "*" to specify the sapce or " *" will do? (2 Replies)
Discussion started by: manishma71
2 Replies

7. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

8. Shell Programming and Scripting

Split File of Number with spaces

How do i split a variable of numbers with spaces... for example echo "100 100 100 100" > temp.txt as the values can always change in temp.txt, i think it will be feasible to split the numbers in accordance to column. How is it possible to make it into $a $b $c $d? (3 Replies)
Discussion started by: dplate07
3 Replies

9. Shell Programming and Scripting

sed: replace string with another string (with spaces)

Hi I have an XML file with strings XABCD, XEFGHX and XIJKLX. I would like to replace XABCDX with "This is the first string", XEFGHX with "This is the second string" and XIJKLX with "This is the third string". What is the best way to implement this? Should I have a file with the data that is... (4 Replies)
Discussion started by: zmfcat1
4 Replies

10. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies
Login or Register to Ask a Question