How to remove leading and trailing spaces for variable in shell script?

Hi

I have variable named tablename. The value to tablename variable has leading and trailing white spaces. How to remove the leading and training white spaces and write the value of the tablename without space to a file using shell script. ( for e.g. tablename= yyy )


INPUT
 
tablename=  yyy  

I want the below output


OUTPUT:
tablename=yyy

Waat shell are you using? What OS? I can give you different ways to do this, but using shell built-ins are the best choice.

As in your last thread, when I asked:

How you remove <space> characters from the start and end of a shell variable depends entirely on what shell (including the version number) of the shell you're using.

Whenever you start a thread asking questions about a shell script, PLEASE tell us what operating system (including release number) and shell (including version number) you're using so we can make suggestions that will work in your environment.

Explain where your variables are initialized (or show us the code that is used to initialize them). The code you showed us in post #1 in this thread (if you're using a standard shell) would initialize the variable tablename to an empty string and put that variable definition in the environment when it invoked the yyyy utility. Once the yyyy utility completed running, the tablename variable would not be defined in your current shell execution environment (unless some other code that you haven't shown us assigned it a value.

If the code you showed us in post #1 was intended to show its initial value instead of how it was set, note that you said that variable had leading AND trailing spaces; but there are no trailing spaces on that line.

Hi

I am using regular .sh file .

I am using Redhat linux as Operating system

As I said in post #6 in your previous thread:

Please tell us the name and version of the shell you are using to run your shell script!

2.6.32- Red hat version and I am using sh

------ Post updated at 11:33 PM ------

Hi

I need few commands which I could try

If there aren't any embedded <space>s in your variable, the command:

tablename=${tablename// /}

will remove all of the leading and trailing (and embedded) <space>s in your variable. If there are embedded <space>s you need to preserve you can use:

while [[ $tablename != ${tablename# } ]]
do	tablename=${tablename# }	# Remove one leading <space> from tablename.
done
while [[ $tablename != ${tablename% } ]]
do	tablename=${tablename% }	# Remove one trailing <space> from tablename.
done
printf 'Updated tablename with zero or more embedded spaces is "%s"\n' "$tablename"

Neither of the above are portable scripts, but all should work in your environment.

For a script meeting your requirements that should work with any shell that performs all of the expansions required by the POSIX standards, you could try

while [ "$tablename" != "${tablename# }" ]
do	tablename=${tablename# }	# Remove one leading <space> from tablename.
done
while [ "$tablename" != "${tablename% }" ]
do	tablename=${tablename% }	# Remove one trailing <space> from tablename.
done
printf 'Updated tablename with zero or more embedded spaces is "%s"\n' "$tablename"

I will try the above options and get back to you

Still not clear to me. /bin/sh should be a POSIX conforming modern shell. Except I don't believe that is likely what you mean. Oh well.

This example is way less efficient than Don's suggestion, but works on modern UNIX systems and Linux AFAIK. In that sense it is pretty much safe.

Remove leading and trailing spaces at the same time:

f=" yyyy  "
# show variable using : to start and : to end
echo ":$f:"
: yyyy  :
# remove blanks
f=`echo "$f" | xargs`
# show variable using : to start and : to end
echo ":$f:"
:yyyy:

"What model is your car?"

"Ford"

"Come again? What model is your car?"

"Blue"

It is not meaningful to say you're using sh. That is a generic term. There are many kinds of sh, and string substitution is one of the ways they specifically differ. If you don't know what your shell is, could you please find out?

Here's a generic method which ought to work in most kinds of sh, but has the severe side-effect of deleting your command-line parameters.

set -- $var
var="$1"
2 Likes

The issue is resolved.Thanks for the support