switz
switz

Reputation: 25188

Get input in shell script

I know to get an input with a shell script you can do

echo "text"
read $VAR

or to get it at runtime

#! /bin/sh

if [ "$1" ]
then
  #do stuff
else
  echo "No input"
fi

But what if I want to get the entire string after I run it, including spaces? I could use quotation marks, but is there any way around this, so I can do: ./script.sh This is a sentence

Upvotes: 0

Views: 2751

Answers (2)

sorpigal
sorpigal

Reputation: 26086

Use

#!/bin/sh

if [ $# -gt 0 ] ; then
    echo "$*"
else
    echo "No input"
fi

But first, think about what you're doing and just pass a single quoted parameter.

Upvotes: 1

Sergei Nikulov
Sergei Nikulov

Reputation: 5110

./script.sh "This is a sentence"

another way is

./script.sh This\ is\ a\ sentence

Upvotes: 0

Related Questions