Score:0

How to pass an input as an argument in a bash script?

bg flag

I have a bash script that gets an input with the read command:

#!/bin/bash

read id

I can run the script $./my_script.sh and after that it wants me to enter the input (id). How can I enter the id as an argument to the script? I mean something like this:

$./my_script.sh id 
Score:1
cn flag
raj

Insert something like this in your script:

id=$1

$1 equals to the first command line argument passed to the script, $2 to the second one, etc.

Of course it would be wise to check first if the command line argument was provided at all. You can use something like this:

if [ -n "$1" ]; then
  id=$1
else
  read id
fi

The command (yes, it is a command) [ checks for a condition (an equivalent command is test). If the first argument is non-empty (-n), then the command id=$1 is executed; otherwise, read id is executed.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.