Skip to main content

Use:

{
  ... your code ...

  exit
}

Bash will read the whole block {} block before executing it, and the exit directive will make sure nothing will be read outside of the code block.

For scripts that are "sourced" rather than executed, use:

{
  ... your code ...

  return 2>/dev/null || exit
}

Use:

{
  ... your code ...

  exit
}

Bash will read the whole {} block before executing it, and the exit directive will make sure nothing will be read outside of the code block.

For scripts that are "sourced" rather than executed, use:

{
  ... your code ...

  return 2>/dev/null || exit
}

Use:

{
  ... your code ...

  exit
}

Bash will read the whole block {} before executing it, and the exit directive will make sure nothing will be read outside of the code block.

For scripts that are "sourced" rather than executed, use:

{
  ... your code ...

  return 2>/dev/null || exit
}
simplify my answer
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26

Simplest solutionUse:

{
  ... your code ...

  exit
}

This way, bashBash will read the whole {} block before executing it, and the exit directive will make sure nothing will be read outside of the code block.

If you do not want to "execute" the script, butFor scripts that are "sourced" rather to "source" itthan executed, you need a different solution. This should work thenuse:

{
  ... your code ...

  return 2>/dev/null || exit
}

Or if you want direct control over exit code:

{
  ... your code ...

  ret="$?";return "$ret" 2>/dev/null || exit "$ret"
}

Voilà! This script is safe to edit, source and execute. You still have to be sure you don't modify it in those milliseconds when it is initially being read.

Simplest solution:

{
  ... your code ...

  exit
}

This way, bash will read the whole {} block before executing it, and the exit directive will make sure nothing will be read outside of the code block.

If you do not want to "execute" the script, but rather to "source" it, you need a different solution. This should work then:

{
  ... your code ...

  return 2>/dev/null || exit
}

Or if you want direct control over exit code:

{
  ... your code ...

  ret="$?";return "$ret" 2>/dev/null || exit "$ret"
}

Voilà! This script is safe to edit, source and execute. You still have to be sure you don't modify it in those milliseconds when it is initially being read.

Use:

{
  ... your code ...

  exit
}

Bash will read the whole {} block before executing it, and the exit directive will make sure nothing will be read outside of the code block.

For scripts that are "sourced" rather than executed, use:

{
  ... your code ...

  return 2>/dev/null || exit
}
deleted 44 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26

Simplest solution:

{
  sleep 10s
... your echocode "test"...

  exit
}

The code block {} and the exit directive is what matters. This way, bash will read the whole {} block before executing it, and the exit directive will make sure nothing will be read outside of the code block.

If you do not want to "execute" the script, but rather to "source" it, you need a different solution. This should work then:

{
  sleep 10s
... your echocode "test"...

  ret="$?";return "$ret"return 2>/dev/null || exit "$ret"
}

Or thisif you want direct control over exit code:

# presuming bash is run with -e (errexit)
{
  sleep 10s
... your echocode "test"...

  returnret="$?";return "$ret" 2>/dev/null || exit 0"$ret"
}

Voilà! This script is safe to edit, source and execute. You still have to be sure you don't modify it in those milliseconds when it is initially being read.

Simplest solution:

{
  sleep 10s
  echo "test"

  exit
}

The code block {} and the exit directive is what matters. This way, bash will read the whole {} block before executing it, and the exit directive will make sure nothing will be read outside of the code block.

If you do not want to "execute" the script, but rather to "source" it, you need a different solution. This should work then:

{
  sleep 10s
  echo "test"

  ret="$?";return "$ret" 2>/dev/null || exit "$ret"
}

Or this:

# presuming bash is run with -e (errexit)
{
  sleep 10s
  echo "test"

  return 2>/dev/null || exit 0
}

Voilà! This script is safe to edit, source and execute. You still have to be sure you don't modify it in those milliseconds when it is initially being read.

Simplest solution:

{
  ... your code ...

  exit
}

This way, bash will read the whole {} block before executing it, and the exit directive will make sure nothing will be read outside of the code block.

If you do not want to "execute" the script, but rather to "source" it, you need a different solution. This should work then:

{
  ... your code ...

  return 2>/dev/null || exit
}

Or if you want direct control over exit code:

{
  ... your code ...

  ret="$?";return "$ret" 2>/dev/null || exit "$ret"
}

Voilà! This script is safe to edit, source and execute. You still have to be sure you don't modify it in those milliseconds when it is initially being read.

deleted 257 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
deleted 100 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
deleted 6 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
edited body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
Mod Moved Comments To Chat
deleted 4 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
typos
Source Link
Stéphane Chazelas
  • 585.4k
  • 96
  • 1.1k
  • 1.7k
Loading
added 25 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
added 346 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
added 36 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
added 63 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
added 145 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
deleted 9 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
added 1 character in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
added 2 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
added 309 characters in body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
edited body
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading
Source Link
VasyaNovikov
  • 1.6k
  • 2
  • 17
  • 26
Loading